[英]prevent click action
我正在為jQuery使用bPopup插件,除單擊外,其他所有工具都工作正常。 當我單擊鏈接時,它將打開一個彈出窗口。 然后,當我單擊另一個鏈接時,它將再次打開一個彈出窗口。
所以我看到e.preventdefault不起作用。 如何使它起作用以防止單擊其他鏈接?
// Semicolon (;) to ensure closing of earlier scripting
// Encapsulation
// $ is assigned to jQuery
;(function($) {
// DOM Ready
$(function() {
// Binding a click event
// From jQuery v.1.7.0 use .on() instead of .bind()
$('#PopUpItUp').live('click', function(e) {
// Prevents the default action to be triggered.
e.preventDefault();
// Triggering bPopup when click event is fired
$('#popup').bPopup({follow: [false, false], position: [310, 25]});
});
});
})(jQuery);
preventDefault()
方法不會驗證您的彈出窗口是否已打開! 它只是防止默認操作a
元素。
不建議使用live方法
我認為您需要這樣做:
$(function() {
var opened = false;
$('#PopUpItUp').click(function(e) {
// Prevents the default action to be triggered.
e.preventDefault();
if(!opened) {
// Triggering bPopup when click event is fired
$('#popup').bPopup({follow: [false, false], position: [310, 25]});
opened = true;
}
});
})(jQuery);
我不知道您為什么選擇使用.live()
方法。您僅需使用.click()
方法,因為您只能擁有一個具有#PopItUp
id的元素
首先,您必須了解e.preventDefault();。 它僅阻止默認操作,例如超鏈接,表單提交操作。
這是您想要的工作示例。
謝謝大家的幫助。 但是我在Fiddle中看到該代碼有效(Jigar Patel),但在我的index.php中卻不起作用...所以我啟動了Firebug,並看到錯誤Reference Error e.PreventDefault()。 e未定義。 因此,在其他沒有PopUpItUp標識的鏈接上會觸發bpopup窗口,這是螢火蟲中斷並顯示錯誤的代碼:
function ShowIMDBInfo(id)
{
$.ajax({
url: 'http://myurladdress/external.php?id='+id,
success: function(data) {
$('#popup').html('<div>' + data + '</div>');
},
error: function(request, error, errormessage) {
$("#messages").html(error + '\n' + errormessage);
}
});
e.preventdefault();
return false;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.