如何获得与此关键字关联的ID ?我已经尝试了SO页面中的几件事,但是在这里,我决定打开这个问题:
$('.anySelector').waypoint(function() {
console.log(this); // entire selector's content
var thisContent = this;
console.log($(thisContent).attr('id')); // undefined
});
如果this
确实是DOM元素(使用jQuery很有可能),那么就像访问id
该元素的属性一样简单。
jQuery( '.anySelector' ).waypoint( function() {
console.log( this.id );
} );
如果您确实需要使用jQuery方式(即使用attr
方法),则必须首先this
转换为jQuery对象:
jQuery( '.anySelector' ).waypoint( function() {
console.log( jQuery( this ).attr( 'id' ) );
} );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句