Jquery 如何为动态html附加事件回调

Jquery 如何为动态html附加事件回调,jquery,Jquery,嗨 我有一个jQuery onload函数,它基于类名在一些内联链接上附加一个鼠标函数调用,我也将使用ajax动态加载一些html内容,但是上面基于类名的函数调用附件不适用于动态html,如何解决这个问题 我的代码如下所示(实际上,在我的代码中,我加载的是图像,而不是简单的链接) $(函数(){ $(“.highlight”).mouseover(函数(){ css(“背景色”、“rgb(255255,0)”); }); $(“.highlight”).mouseout(函数(){ css(“背

嗨 我有一个jQuery onload函数,它基于类名在一些内联链接上附加一个鼠标函数调用,我也将使用ajax动态加载一些html内容,但是上面基于类名的函数调用附件不适用于动态html,如何解决这个问题

我的代码如下所示(实际上,在我的代码中,我加载的是图像,而不是简单的链接)

$(函数(){ $(“.highlight”).mouseover(函数(){ css(“背景色”、“rgb(255255,0)”); }); $(“.highlight”).mouseout(函数(){ css(“背景色”、“rgb(255255)”); }); }); $(函数(){ //ajax调用 //在动态div中设置ajax返回值 $(“.dynamic”).html(“+新日期()+”); }); jQuery live()功能可能满足您的需要


您需要使用适用于任何未来元素以及当前元素的绑定事件。

您需要


这将在每个
上添加鼠标盖,然后再附加它

很高兴听到它。。。您可以感谢jQuery播客让我了解了它的一切:-) $(function(){ $(".highlight").mouseover(function(){ $(this).css("background-color", "rgb(255,255,0)"); }); $(".highlight").mouseout(function(){ $(this).css("background-color", "rgb(255,255,255)"); }); }); $(function(){ //ajax call // set the ajax return value inside dynamic div $(".dynamic").html(""+new Date()+""); }); <body> <div> <a href="#" class="highlight">link1</a> <a href="#" class="highlight">link2</a> <a href="#" class="highlight">link3</a> </div> <div class="dynamic"></div> </body<
$('.dynamic a').live('mouseover', function(){
   $(this).css({border: '1px solid red'});
});