jquery using waypoint get id of element

两盒软妹~` 提交于 2019-12-23 19:06:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!