javascript 将 $this 传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3373723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:00:06  来源:igfitidea点击:

Pass $this to function

javascriptjquerythis

提问by TMP file guy

How can I pass the $this (the self keyword) to a function in Jquery

如何将 $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'
    }       
});

回答by Josh K

Wrapping it in $()makes it a jQuery object. You would want to do something like

将它包装起来$()使其成为一个 jQuery 对象。你会想做类似的事情

submit_form(this);

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

回答by ebynum

The thiskeyword cannot be set in JavaScript. It is automatically generated by JavaScript and "always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of".
http://www.quirksmode.org/js/this.html

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

You have one problem in your code (trying to name the parameter of the submit_form() function to this). However, the way your code is laid out doesn't make it clear whether you're intending to pass along the clicked anchor wrapped as a jQuery object or as the DOM node for the anchor.

您的代码中存在一个问题(试图将 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
    }
});

On a mostly unrelated note, you'll probably want to either return false;or use event.preventDefault() to keep the page from following the hrefon the anchor that was clicked. You can do that as follows:

在一个几乎不相关的注释中,您可能希望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
    }); 
});

回答by TMP file guy

Here is what worked for me.

这对我有用。

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

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

回答by Peter

Yes, thiscan be set easily. See Function.call()and Function.apply().

是的,可以轻松设置。见Function.call()Function.apply()

回答by Fosco

Try 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'


        }        

    }); 

回答by Aren

Just pass it as a normal variable?

只是将其作为普通变量传递?

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

}

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