我有以下jQuery代码:
$('a[rel=close]').click(function() {
alert('Close click!');
$('div#purchasePanel').hide();
});
用于:
<div id="purchasePanel">
<a href="#close" rel="close"><li> Close </li></a>
</div>
警告()永远不会被调用。
使用AJAX显示和显示DIV内容,效果很好。只是这个讨厌的关闭按钮拒绝......
答案 0 :(得分:3)
最有可能是在注册处理程序后添加链接。
解决方案是使用live event:
$('a[rel=close]').live('click', function(e) {
e.preventDefault();
alert('Close click!');
$('div#purchasePanel').hide();
});
答案 1 :(得分:0)
如果使用AJAX生成DIV,则应live
而不是click
。
答案 2 :(得分:0)
如果您的脚本标记位于链接之前,则处理程序可能未附加。试试这个:
<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>