Javascript 将$this传递给函数

Javascript 将$this传递给函数,javascript,jquery,this,Javascript,Jquery,This,如何将$this(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(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'
    }       
});
试试这个:

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

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


        }        

    }); 
将其包装在
$()
中使其成为jQuery对象。你会想做一些像

submit_form(this);

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

只是把它作为普通变量传递

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

}

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

无法在JavaScript中设置
关键字。它由JavaScript自动生成,“总是指我们正在执行的函数的“所有者”,或者更确切地说,指函数作为方法的对象”。
-

代码中有一个问题(尝试将submit_form()函数的参数命名为this)。但是,代码的布局方式并不清楚您是打算将单击的锚作为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
    }
});
在一个几乎不相关的注释中,您可能希望
返回false
或使用event.preventDefault()防止页面跟随单击的锚上的
href
。您可以按如下方式执行此操作:

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

这是对我有用的东西

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

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

是的,这很容易设置。请参见
Function.call()
Function.apply()

您已经正确地执行了。只需将
函数submit\u form(this){}
中的变量重命名为类似
函数submit\u form(element){}
的其他变量。请按预期方式使用javascript(无需将上下文作为参数传递):
提交表单。调用(this)
提交表单。应用(this)
,如果你只懂jQuery,甚至可以使用jQuery:
jQuery.proxy(提交表单,这个)