문제

I'm using event.stopPropagation(), but I am having problems using .live() handler on the parent.

//does not work
$("#testlink").live({ 
    click: function(event) 
    { 
        alert('testlink'); 
    } 
}); 

//works great! 
$("#testlink").click(function() { 
  alert('testlink'); 
});

I have to use .live() because the content is loaded with AJAX.

Can someone help me with this problem?


How can i use more classes or ids like this?

$('#div1', '#div2', '#div3').on('click', 'a.testlink', function(){
   alert('testlink'); 
});

How is this possible?

도움이 되었습니까?

해결책

Use on() (as @techfoobar mentioned, live() is deprecated in version 1.7 and removed as of version 1.9) and event delegation if the content is dynamically generated:

$('#container').on('click', '#testlink', function(){
   alert('testlink'); 
});

Where #container is an element containing the dynamically loaded #testLink element (you could use document, but using a container element here reduces the search scope and increases efficiency).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top