简体   繁体   English

使用JavaScript“ this”作为jquery选择器

[英]Using javascript “this” for jquery selector

In this code, we can obtain value of element id as 2 differnt way and both way return resultat 在此代码中,我们可以获得元素id的值作为2种不同的方式,并且两种方式都返回resultat

$("#my_div").on("click", function () {
    alert( $(this).attr("id") );

    alert( this.id );
});

But i interest, second way, is it proper in this case? 但是我有兴趣,第二种方式,在这种情况下是否合适? I ask this, because in code we use jquery selector and for jquery selector, write clear javascript: this is justified and will it work always? 我问这个问题,因为在代码中我们使用jquery选择器,对于jquery选择器,请编写清晰的javascript: 是有道理的,它将始终有效吗? or may be better use jquery $(this) for jquery selector? 还是更好地使用jquery $(this)作为jquery选择器? or not differnce? 或没有差异?

this.id will give you the internal DOM element property while $(this).attr("id") returns the value of 'id' attribute. this.id将为您提供内部DOM元素属性,而$(this).attr("id")返回“ id”属性的值。

The alternative to this.id in jQuery is using prop() method: $(this).prop("id") . jQuery中this.id的替代方法是使用prop()方法: $(this).prop("id") However, using pure this.id construction will be easier and faster. 但是,使用纯this.id构造将更加容易和快捷。

The main jQuery constructor can take a number of different types of argument. jQuery主构造函数可以采用许多不同类型的参数。

In that context, this is not a selector, it is an HTMLElementNode (which jQuery calls an element ). 在这种情况下, this不是选择器,而是HTMLElementNode (jQuery将其称为element )。

This is perfectly OK and documented . 这是完全可以的,并且有文件记录

There is no need to spend the resources in wrapping an element in a jQuery object if you are just going to grab its ID. 如果您只是要获取其ID,则无需花费资源将元素包装在jQuery对象中。 There aren't any compatibility problems between browsers for that. 浏览器之间没有任何兼容性问题。

Grabbing the id property of an HTMLElementNode takes less code and is faster then wrapping the whole thing in jQuery. 抓取HTMLElementNode的id属性需要更少的代码,并且比将整个东西包装在jQuery中要快。

Yes, your second way is proper. 是的,您的第二种方法是正确的。

$("#my_div").on("click", function () {
    // this is available within this function
    alert( this.id );
});

this refers to an HTMLDOMElement within the function, and wrapping that this within $() will give you an jQuery object. this指的是HTMLDOMElement函数中,并将它包装this$()会给你一个jQuery对象。

If you define another function within the click handler. 如果您在click处理程序中定义了另一个函数。 Ex: 例如:

$("#my_div").on("click", function () {
    // keep reference of this
    var that = this;
    function test() {
     // this will not available here directly
     // instead of that you can use reference
     alert(that.id);
    }
});

And $(this).attr('id') , this.id or $(this).prop('id') will give you the same result. $(this).attr('id')this.id$(this).prop('id')会得到相同的结果。

For choosing the best method is not always clear. 选择最佳方法并不总是很清楚。

In this case you want the this.id , because the other solution requires much more calls behind the scenes (obvious calls to jQuery and attr ). 在这种情况下,您需要this.id ,因为另一个解决方案需要在后台进行更多调用(对jQueryattr明显调用)。

If you need more information that mite differ from browser, you want the jQuery way. 如果您需要更多与浏览器不同的信息,可以使用jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM