嗨,我没有分享代码。问题是在这段代码中,请看一下。(问题:每次我都想打开facebox,即使是来自ajax的帖子,facebox也不能正常工作。我想问一下如何在这个事件中使用jQuery委托事件?)?
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="facebox/facebox.css" type="text/css" />
</head>
<body>
<ul id="items">
<li> <a href='#'> Clone Object </a> </li>
<li> <a href='#test' rel='facebox'> Click to open facebox </a> </li>
</ul>
<div id="test" style="display:none;">this is my test div</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="facebox/facebox.js"></script>
<script type="text/javascript">
jQuery('a[rel*=facebox]').facebox();
$("#items li").delegate('a', 'click', function(){
$(this).parent().append('<li><a href="#test" rel="facebox"> New Element ( problem: it should open facebox) </a></li>');
return false;
});
</script>
</bdoy>
</html>发布于 2010-09-26 11:42:43
首先,通过切换以下内容,更改为使用.live():
jQuery('a[rel*=facebox]').facebox();为此:
jQuery('a[rel*=facebox]').live('click', function(e) {
$.facebox({ div: this.hash });
e.preventDefault();
});或者.delegate()版本:
$('#items').delegate('a[rel*=facebox]', 'click', function(e) {
$.facebox({ div: this.hash });
e.preventDefault();
});然后,您的.parent()调用将<li>附加到<li>,而应该替换:
$(this).parent()使用.cloest()时,它会上升到<ul>,并将其作为子级附加到那里,如下所示:
$(this).closest("ul")您可以在这里使用上述更改(没有图像)测试更新的版本。。
https://stackoverflow.com/questions/3797599
复制相似问题