해당 섹션 (이 예제에서는 #one 및 #two) 내의 객체에 이벤트 리스너를 첨부해야하는 클래스를 작성하고 있지만 클래스의 변수를 전달하는 데 문제가 있습니다. jQuery 이벤트 리스너를 설정하십시오.자바 스크립트 클래스 및 jQuery 이벤트 리스너
<script type="text/javascript">
function myClass(id) {
this.id = id;
$(document).ready(function(){
$(this.id + ' .filter').click(function(){ alert("You clicked a filter"); });
});
}
one = new myClass('#one');
two = new myClass('#two');
</script>
<div id="one">
<a href="javascript://" class="filter">I am link one</a>
</div>
<div id="two">
<a href="javascript://" class="filter">I am link two</a>
</div>
... 불행히도 변수 범위가 나와 동의하지 않습니다; this.id
은 $(document).ready()
에서 액세스 할 수 없으므로 이벤트 리스너는 실행되지 않습니다. 어떻게 접근 할 수 있습니까?
우수, 감사! –