2010-07-30 35 views
10

如何传递这个(self关键字)在$一个函数在jQuery中

$(document).ready(function() { 

    $('.nav a').click(function() { 
     var direction = $(this).attr("name"); 
     submit_form($(this)) 
    }); 

    function submit_form(this) 
    { 
     // do some stuff with 'this' 
    }  
}); 
+4

你已经做得正确。只需将'function submit_form(this){}'中的变量重命名为'function submit_form(element){}''。 – 2010-07-30 17:01:54

+1

请以使用javascript的方式使用(不需要传递上下文作为参数):'submit_form.call(this)','submit_form.apply(this)',或者甚至使用jQuery,如果这是唯一的你明白了:'jQuery.proxy(submit_form,this)' – 2012-10-09 22:12:26

回答

16

$()结束语使它一个jQuery目的。你会想要做类似于

submit_form(this); 

function submit_form(obj) 
{ 
    // Work with `obj` as "this" 
    // Or wrap it in $() to work with it as jQuery(this) 
} 
+1

$(重要的一点!) – 2010-07-30 17:19:09

1

试试这个:

$(document).ready(function() 
    { 
     $('.nav a').click(function() { 
      var direction = $(this).attr("name"); 
      submit_form(this); 
     }); 

     function submit_form(myObject) 
     { 
      // do some stuff with 'myObject' 


     }   

    }); 
0

只是将它作为一个正常变量传递?

function submit_form(context) 
{ 
    context.html("Boo!"); 

} 

/// Then when you call it: 
submit_form($(this)); 
5

this关键字不能在JavaScript中进行设置。它是由JavaScript自动生成的,并且“总是指向我们正在执行的函数的”所有者“,或者更确切地说,是指函数是”的方法“的对象。
http://www.quirksmode.org/js/this.html

你在你的代码的一个问题(试图命名submit_form()函数的这个参数)。然而,你的代码的布局方式并没有说明你是否打算传递包装为jQuery对象的点击的锚或作为锚的DOM节点。

$(document).ready(function() { 
    $('.nav a').click(function() { 
     $anchor = $(this);      // Capture the jQuery-wrapped anchor for re-use; 'this' is an anchor because it matched $('.nav a') 
     var direction = $anchor.attr("name"); // $variable_name is a standard pattern in jQuery to indicate variables that are jQuery-wrapped instead of DOM objects 

     submit_form_jquery($anchor);   // Providing versions of submit_form function for passing either jQuery-wrapped object or DOM object 
     submit_form_dom(this);     // Pick the one you prefer and use it 
    }); 

    function submit_form_jquery($my_anchor) { // Function with well-named parameter expecting a jQuery-wrapped object 
     // do some stuff with '$my_anchor' 
     // $my_anchor here is assumed to be a jQuery-wrapped object 
    } 

    function submit_form_dom(anchor) {   // Function named expecting a DOM element, not a jQuery-wrapped element 
     // do some stuff with 'anchor' 
     // anchor here is assumed to be a DOM element, NOT wrapped in a jQuery object 
    } 
}); 

在一个大部分是不相关的笔记,你可能会想要么return false;或使用event.preventDefault()从下面的被点击的锚href保持页面。你可以这样做,如下:

$(document).ready(function() { 
    $('.nav a').click(function(event) { 
     event.preventDefault(); 
     // And now do what you want the click to do 
    }); 
}); 
1

这是什么为我工作。

$(document).ready(function() 
{ 
    $('.nav a').click(function() { 
     submit_form(this) 
    }); 

    function submit_form(thisObject) 
    { 
     var direction = $(thisObject).attr("name"); 
    }  
}); 
1

是的,这可以很容易地设置。见Function.call()Function.apply()