开发者

jQuery Delegation with Facebox

hi, i am not sharing code. the problem is in this code please take a look. (problem: I want to open facebox every time when post even come from ajax facebox is not working properly. I want to ask how to use jQuery delegation event with this event? )

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="st开发者_开发技巧ylesheet" 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>


A few things, first change to using .live(), by switching this:

jQuery('a[rel*=facebox]').facebox();

For this:

jQuery('a[rel*=facebox]').live('click', function(e) {
  $.facebox({ div: this.hash });
  e.preventDefault();
});

Or the .delegate() version:

$('#items').delegate('a[rel*=facebox]', 'click', function(e) {
  $.facebox({ div: this.hash });
  e.preventDefault();
});

Then your .parent() call is appending an <li> to an <li>, instead you should replace:

$(this).parent()

With .cloest() so it goes up to the <ul> and appends it as a child there, like this:

$(this).closest("ul")

You can test out the updated version with the above changes (without images) here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜