使用Jquery禁用锚标记 [英] disable anchor tag using Jquery
本文介绍了使用Jquery禁用锚标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个与锚标记关联的图像,一旦用户点击弹出窗口加载的图像。我想禁用此锚标记。
I have a image associated with the anchor tag, once the user clicks the image a popup loads. I want to disable this anchor tag.
html代码如下所示:
The html code looks like:
<a href="#" class="openModalLink">
<img style="vertical-align: middle; border: none" width="9%" alt="" id="imgmap" class="zoom" /></a>
我尝试过以下代码,但似乎无法运作
I have tried the below codes but doesn't seem to work
$(".openModalLink").off("click");
$(".openModalLink").attr("disabled", true);
$(".openModalLink").attr("disabled", "disabled");
感谢您的回复
推荐答案
你可以这样做
$('.openModalLink').click(function(event){
event.preventDefault();
});
另请参阅 docs
编辑:
启用和禁用锚标记
function disabler(event) {
event.preventDefault();
return false;
}
$('#enable').click(function(){
$('.openModalLink').unbind('click',disabler);
});
$('#disable').click(function(){
$('.openModalLink').bind('click',disabler);
});
DEMO
编辑2:
截至jquery 1.7 .on()
和 .off()
优先于绑定和取消绑定以附加和删除元素上的事件处理程序
As of jquery 1.7 .on()
and .off()
are preferred over bind and unbind to attach and remove event handlers on elements
$('#enable').click(function() {
$('body').off('click', '.openModalLink', disabler);
});
$('#disable').click(function() {
$('body').on('click', '.openModalLink', disabler);
});
这篇关于使用Jquery禁用锚标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文