开发者

Jquery live not working in IE

I have some code that works fine in FF but not in IE. I have read other posts saying that the Jquery live method does not support change events but I am using a click event. Here is my code, it is inside $(document).ready(function():

$("a").live("click", function(e) { 
        alert("link clicked");
        //do stuff
    });

If FF the alert is fired but not in IE. When I use $("a").click it works fine, the problem is that I need to the function to be applied to links that do not exist when the page is first loaded (they will be created via aja开发者_开发百科x calls at a later stage).

Do I have any options here. We are using jquery-1.4.1.min.js.

Thanks in advance


if those links are within a specific content, you can use:

$('#link_container_id').delegate('a', 'click', function(e){
   alert('link clicked');
});

.delegate() will watch if there are any events (click in your case) bubbling up, if so it checks for the target and compares it to 'a' in your case. Should work, but untested.


Elements should exist in the DOM at the moment you attach the live event. If later they are recreated (for example in an ajax callback) you don't need to reattach the event handler again. If elements don't exist at page load you can attach the live event when they are loaded, but if you do this then probably you no longer need the live event as you can directly attach the click event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜