CSS: Is a hidden object clickable?

2019-01-14 15:05发布

问题:

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

When the display property 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 hidden element still responds to mouse events.

回答1:

With display: none it is still part of the DOM. It just isn't rendered in the viewport.

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

jsFiddle.

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



回答2:

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.



回答3:

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').click(function() {
    alert('Hello')
});
$('div').click();

jsfiddle