문제

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 );
} );

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' ) );
} );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top