CSS:隐藏的对象是否可点击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5659563/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:14:53  来源:igfitidea点击:

CSS: Is a hidden object clickable?

cssvisibilitydisplay

提问by euphoria83

If the visibilityproperty of the style of an HTML element is set to hidden, is it still clickable?

如果visibilityHTML 元素的样式属性设置为hidden,它是否仍然可以点击?

When the displayproperty is set to none, the element is not even part of the DOM tree, so that is not a problem. But I was wondering if a hiddenelement still responds to mouse events.

display属性设置为 时none,该元素甚至不是 DOM 树的一部分,因此这不是问题。但我想知道hidden元素是否仍然响应鼠标事件。

回答by alex

With display: noneit isstill part of the DOM. It just isn't rendered in the viewport.

随着display: none仍然DOM的一部分。它只是没有在视口中呈现。

As for clicks on elements with visibility: hidden, the events are notfired.

对于带有visibility: hidden的元素的点击,不会触发事件。

jsFiddle.

js小提琴

$('div').click(function() {
    alert('Hello')
});
div {
    width: 100%;
    height: 100%;
    visibility: hidden; 
}
<div>abc</div>

回答by Jin Thakur

Making div hidden or display none just makes it un clickable to user. But in real its still an element in dom and you can click it with another java script/jquery like this.

使 div 隐藏或不显示只会使用户无法点击它。但实际上它仍然是 dom 中的一个元素,您可以使用另一个这样的 java 脚本/jquery 单击它。

$('div').click(function() {
    alert('Hello')
});
$('div').click();

jsfiddleenter image description here

提琴手在此处输入图片说明

回答by ADW

No.

不。

An element such as a hyperlink can't be clicked (and the link followed) if the visibility is set to hidden. Similarly, onclick events won't be fired.

如果可见性设置为隐藏,则无法单击诸如超链接之类的元素(并且无法单击该链接)。同样,不会触发 onclick 事件。