Javascript 显示:无;在浏览器中显示“无”

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

display:none; displays 'none' in browser

javascripthtmlcss

提问by Web_Designer

This jsFiddle exampleworks in Google Chrome, but in Internet Explorer then when the close icon is clicked the browser removes the pop-up element but results in the text 'none' being displayed in the browser window. Please explain how I can resolve this issue.

这个 jsFiddle 示例适用于谷歌浏览器,但在 Internet Explorer 中,当点击关闭图标时,浏览器会删除弹出元素,但会导致浏览器窗口中显示文本“无”。请解释我如何解决这个问题。

HTML:

HTML:

<div id="popup">
    <!-- Close popup link -->
    <a href="javascript:document.getElementById('popup').style.display='none';">X</a>
</div>

回答by amit_g

Use onclick for the event handler instead of href http://jsfiddle.net/AE2X3/4/

使用 onclick 作为事件处理程序而不是 href http://jsfiddle.net/AE2X3/4/

<div id="popup">
        <a href="#" onclick="document.getElementById('popup').style.display='none';return false;" id="close_popup"></a>
        <p>This is a pop-up.</p>
</div>

回答by AgentConundrum

I think what's happening is that the assignment is returning its result, and the browser is then displaying that. If you add void(0)to the end of your JavaScript, it won't be displayed.

我认为正在发生的事情是分配正在返回其结果,然后浏览器将显示该结果。如果您添加void(0)到 JavaScript 的末尾,它将不会显示。

Let me add that amit_g's answeris more correct than mine. He correctly points out that this sort of behaviour belongs in the OnClickhandler, not in the hrefattribute.

让我补充一点,amit_g 的答案比我的更正确。他正确地指出这种行为属于OnClick处理程序,而不属于href属性。

回答by Matt Ball

This works:

这有效:

<div id="popup">
        <a href="javascript:void(0);" onclick="this.parentElement.style.display='none';" id="close_popup"></a>
        <p>This is a pop-up.</p>
</div>

Demo

演示