问题
i'am using jquery waypoints (http://imakewebthings.com/jquery-waypoints/#docs) to check if an element is the view of the browser, here the html:
<div class="container" id="container_1">1. Container</div>
<div class="container" id="container_2">2. Container</div>
<div class="container" id="container_3">3. Container</div>
<div class="container" id="container_4">4. Container</div>
here the js
$('#container_1').waypoint(function() {
console.log("container 1 is visible");
});
this works fine!
But is it possible to find out the current id of the element which is in view and the waypoint get fired? something like this:
$('.container').waypoint(function() {
console.log("id of element: " + $(this).attr('id');
});
thanks!
回答1:
You are missing parenthesis: )
at the end of console.log otherwise it would work.
$('.container').waypoint(function() {
console.log("id of element: " + $(this).attr('id'));
});
If you are confusing, you can use like this too:
$('.container').waypoint(function() {
var $this = $('.container');
console.log("id of element: " + $this.attr('id'));
});
回答2:
In waypoints.js I've found that this
refers to a waypoints internal object. If you console.log it though, you easily find how to select that element with jquery.
handler: function (direction){
var DOMElement = $(this.element);
console.log($(this.element).attr('data-id');
}
Also... element attribute ID return undefined
来源:https://stackoverflow.com/questions/21232457/jquery-using-waypoint-get-id-of-element