javascript - does a click event fire on an element if it's not visible?

巧了我就是萌 提交于 2019-12-25 02:52:31

问题


If you change the visibility of an element visibility: hidden, will a click event still fire if the user clicks it?

I want to "hide" an element (i.e. <span>) and disable the click event from firing, but retain is position in the normal flow of the document. So display: none; won't work since it removes the document from the normal flow, but was wondering what are my other options via CSS without actually handling the click event and using preventDefault()?


回答1:


No it won't fire when visibility:hidden, here is proof :)

jsFiddle

JS

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

CSS

div {
    background-color:red;
    width:100px;
    height:100px;
}

.hidden {
    visibility:hidden
}



回答2:


Yes visibility hidden disables the click event.

To test just right click a clickable element on this page with a web-kit browser, apply a style of visibility: hidden and you will be unable to click it.



来源:https://stackoverflow.com/questions/15708418/javascript-does-a-click-event-fire-on-an-element-if-its-not-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!