javascript 使用 Jquery 禁用 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5142657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable div using Jquery
提问by Shwetanka
I have the event onClick=window.location.href = 'some url stored in div.
我有活动onClick=window.location.href = 'some url stored in div。
Divcontains images as well as text.
Div包含图像和文本。
I want to show preview using this div, but want clicking disabled on this divbecause it is taking to the specified location.
我想使用 this 显示预览div,但希望在此上禁用单击,div因为它正在进入指定位置。
I tried it like this:
我是这样试的:
("#preview").disabled= true;
("#preview").disabled= 'disabled';
("#preview").children().disabled= true;
("#preview").children().disabled= 'disabled';
But none of this is working in firefox 3.6. Somebody please help to solve this problem.
但这些都不适用于firefox 3.6. 有人请帮助解决这个问题。
回答by jenson-button-event
$('#preview').unbind('click');
回答by Sergio Ramirez
If you want to disable all the div's controls, you can try adding a transparent div on the div to disable, you gonna make it unclickable, also use fadeTo to create a disable appearance.
如果你想禁用所有 div 的控件,你可以尝试在 div 上添加一个透明的 div 来禁用,你会使其不可点击,也可以使用fadeTo 创建禁用外观。
try this.
试试这个。
$('#DisableDiv').fadeTo('slow',.6);
$('#DisableDiv').append('<div style="position: absolute;top:0;left:0;width: 100%;height:100%;z-index:2;opacity:0.4;filter: alpha(opacity = 50)"></div>');
回答by jAndy
First, why would you use an inline eventhandler while using a library like jQuery. Do it the unobtrusive way and bind all event handlers with Javascript.
首先,为什么要在使用像 jQuery 这样的库时使用内联事件处理程序。以不显眼的方式执行此操作,并使用 Javascript 绑定所有事件处理程序。
This actually will also help you with your problem, because then you can unbindand rebindan event handler very easily:
这实际上也将帮助您解决问题,因为这样您就可以非常轻松地解除绑定和重新绑定事件处理程序:
function myclick() {
window.location.href = 'some url';
}
// bind the click event handler
$(function() {
$('#preview').bind('click', myclick);
});
// unbind the click event handler
$('#preview').unbind('click', myclick);
That way, you can add and remove the functionality, but it won't change any visible change to the div node. You would also have to add a styleor css classto let an user know that something changed.
这样,您可以添加和删除功能,但不会更改div 节点的任何可见更改。您还必须添加样式或css 类以让用户知道某些内容已更改。
回答by Nelson Rothermel
You should be able to simply do:
您应该能够简单地执行以下操作:
// Unbind all click events for all elements
("#preview *").unbind("click");
// Unbind all click events for all elements
("#preview *").unbind("click");
The other thing you could do is what modals often do with the background: place a non-clickable div on top of the other content.
你可以做的另一件事是模态通常对背景做的事情:在其他内容的顶部放置一个不可点击的 div。
回答by Halil ?zgür
$("#preview div").onClick = function(){};
回答by Eric Yin
Try
尝试
$('#the_div_id *').unbind('click');

