开发者

Hiding an AJAX DIV tag... problems

开发者 https://www.devze.com 2023-02-16 23:39 出处:网络
I have the following开发者_开发知识库 jQuery code: $(\'a[rel=close]\').click(function() { alert(\'Close click!\');

I have the following开发者_开发知识库 jQuery code:

$('a[rel=close]').click(function() {
        alert('Close click!');
        $('div#purchasePanel').hide();
    });

This is used with:

<div id="purchasePanel">
<a href="#close" rel="close"><li> Close </li></a>
</div>

The alert() never gets called.

The DIV contents is shown and displayed using AJAX, which works fine. It's just this pesky close button that refuses to...


Most likely you are adding the link after registering the handler.

The solution is using a live event:

$('a[rel=close]').live('click', function(e) {
    e.preventDefault();
    alert('Close click!');
    $('div#purchasePanel').hide();
});


If the DIV is generated using AJAX you should live instead of click.


If your script tag is before the link the handler is probably not getting attached. Try this:

<script>
// wait until everything has loaded
$(document).ready(function(){
    $('a[rel=close]').click(function(e){
            e.preventDefault(); // needed to stop from loading the link
            alert('Close click!');
           $('#purchasePanel').hide();
    });
});
</script>
0

精彩评论

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

关注公众号