2011-03-18 32 views
0

嗨 我有一个jQuery的onload函数,附加一个基于类名的一些内联链接的鼠标功能调用,也我会加载一些html内容动态使用ajax,但基于以上的类名函数调用附件不能正常使用的动态HTML,如何解决这个..如何附加动态html的事件回调

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

 

$(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< 

谢谢 venkat papana

回答

2

jQuery live()功能可能会满足您的需求。

http://api.jquery.com/live/

+0

这是伟大的工作添加鼠标悬停.. :)非常感谢你戴夫 – 2011-03-18 18:29:17

+0

很高兴听到它! ...你可以感谢jQuery podcast了解它的任何事情:-) – 2011-03-18 18:32:57

0

您需要绑定使用http://api.jquery.com/live/它适用于任何未来的元素,以及当前与你的某个事件。

+0

是的,它是工作谢谢蚂蚁。 – 2011-03-18 18:32:18

0

你需要.live() method

$('.dynamic a').live('mouseover', function(){ 
    $(this).css({border: '1px solid red'}); 
}); 

这将在每个<a>你追加之后

+0

谢谢@teneff – 2011-03-18 18:32:36