使用航点和jquery遇到一些麻烦$(this)

时间:2015-06-12 07:35:00

标签: javascript jquery jquery-waypoints

我使用的是最新的航点库。每次路点都会传递program-block,它应该将属性data-index的值记录到控制台,但每次发生这种情况时都会输出undefinded

我该如何处理?

<div class="program-block" data-index="1"></div>
<div class="program-block" data-index="2"></div>
<div class="program-block" data-index="3"></div>
<div class="program-block" data-index="4"></div>
<div class="program-block" data-index="1"></div>

var waypoints = $('.program-block').waypoint({
    handler: function(direction) {
        console.log($(this).data('index'));
    }
});

Here's my code(但没有工作样本):

这里是航点网站的link

1 个答案:

答案 0 :(得分:0)

在航点3中this不是对HTML元素的引用。它是对Waypoint实例的引用。要获取基础元素,您必须使用this.element