jquery使用waypoint获取元素的id

时间:2022-05-12 09:18:07

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:

我使用jquery waypoints(http://imakewebthings.com/jquery-waypoints/#docs)检查一个元素是否是浏览器的视图,这里是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

这里是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:

但是有可能找出视图中的元素的当前id并且路点被触发吗?像这样的东西:

$('.container').waypoint(function() {
         console.log("id of element: " + $(this).attr('id');
    });

thanks!

谢谢!

2 个解决方案

#1


1  

You are missing parenthesis: ) at the end of console.log otherwise it would work.

你在console.log的末尾缺少括号:),否则它会起作用。

$('.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


1  

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.

在waypoints.js中,我发现这是指一个航路点内部对象。如果你使用console.log,你很容易找到如何用jquery选择那个元素。

handler: function (direction){
    var DOMElement = $(this.element);
    console.log($(this.element).attr('data-id');
}

Also... element attribute ID return undefined

另外...元素属性ID返回undefined

#1


1  

You are missing parenthesis: ) at the end of console.log otherwise it would work.

你在console.log的末尾缺少括号:),否则它会起作用。

$('.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


1  

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.

在waypoints.js中,我发现这是指一个航路点内部对象。如果你使用console.log,你很容易找到如何用jquery选择那个元素。

handler: function (direction){
    var DOMElement = $(this.element);
    console.log($(this.element).attr('data-id');
}

Also... element attribute ID return undefined

另外...元素属性ID返回undefined