从jQuery获取一个#id就是这个 [英] Getting an #id from jQuery's this
本文介绍了从jQuery获取一个#id就是这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取与 this 关键字相关联的ID?我已经尝试了SO页面中的几件事,但是在这里,我决定打开这个问题:
How do I get the ID associated with the this keyword? I've tried several things from SO pages but here is where I've decided to open the question:
$('.anySelector').waypoint(function() {
console.log(this); // entire selector's content
var thisContent = this;
console.log($(thisContent).attr('id')); // undefined
});
推荐答案
If this
really is a DOM Element (which is very probable using jQuery) it is as simple as just accessing the id
property of the element.
jQuery( '.anySelector' ).waypoint( function() {
console.log( this.id );
} );
如果您确实需要使用jQuery方式(即使用attr
方法),则必须先将this
转换为jQuery对象:
If you really need to go the jQuery way (i.e. using the attr
method) you must turn this
into a jQuery object first:
jQuery( '.anySelector' ).waypoint( function() {
console.log( jQuery( this ).attr( 'id' ) );
} );
这篇关于从jQuery获取一个#id就是这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文