2017-12-13 3 views
0
<body> 
<div id="wayp-1" class="tracked">WAY</div> 
<div id="wayp-2" class="tracked">WAY</div> 
<div id="wayp-3" class="tracked">WAY</div> 
<script src="lib/jquery.min.js"></script> 
<script src="lib/jquery.waypoints.min.js"></script> 
<script> 
    $('.tracked').waypoint(function(){ 
     alert($(this).attr('id')); 
    }) 
</script> 

jQuery 메서드 .attr() 'id'가 정의되지 않은 요소를 반환합니다. 웨이 포인트 플러그인이 코드는 undefined를 반환 어떤 이유로

을 추적하는 데 사용됩니다. 웨이 포인트 문서를 바탕으로

+0

jsfiddle을 만들어주세요 –

+0

'$ (this)'이 (가) 스크롤 된 요소를 나타 냅니까? – Unknown

+1

첫 번째 : 전체 오류나 그 정의되지 않은 것은 무엇입니까? .. 두 번째 :이 플러그인의 링크는 어디에 있습니까? –

답변

0

이 플러그인은 밝혀 요소로 $(this)을 허용하지 않습니다, 대신 그들은 당신이 jQuery를을 사용하는 경우 this.element을 사용하도록 제안.

jQuery를 :

$('.tracked').waypoint(function(){ 
    handler: function(direction) { 
     notify(this.element.id + ' hit') 
    } 
}); 

를 또는 순수 자바 스크립트 (하지 jQuery를) 사용하는 경우 당신은 요소 속성 $(this)을하지 액세스 할 수 this을 사용할 수 있습니다 : 귀하의 코드는이 방법으로 편집해야

var waypoint = new Waypoint({ 
    element: document.getElementsByClassName('tracked'), 
    handler: function(direction) { 
     notify(this.id + ' hit') 
    } 
}); 
관련 문제