Click event only works on first click after pageload
I use this popup plugin for jquery and get some strange开发者_StackOverflow behaviour. I have contacted the creator but got no answer and would like to ask here if there is any general problem with my code.
The setup: Regular html links that open a page in a new tab (for JS disabled users).
The js below to catch the link click and animate a popup instead of following the link.
The problem: It works the first click on every page load but therafter a click follows the href instead.
js
$("a[class*='popupbox-link']").live("click",function (e) {
if($("#popupbox").length == 0){
$('body').append('<div id="popupbox"><a class="bClose">Stäng</a><div id="pContent"></div></div>');
}
var url = this.href;
$("#popupbox").bPopup({contentContainer:'#pContent', loadUrl: url });
e.stopPropagation();
return false
});
try using delegate instead of live
Try to prevent the event propagation
$("a[class*='popupbox-link']").live("click",function (e) {
e.stopPropagation();
if($("#popupbox").length == 0){
$(‘body’).append(‘Stäng‘);
}
var url = this.href;
$("#popupbox").bPopup({contentContainer:'#pContent', loadUrl: url });
return false;
});
精彩评论