Javascript 如何在javascript中禁用iframe?

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

How to disable an iframe in javascript?

javascripthtmliframe

提问by Jagadesh

How can an iframe be disabled using Javascript, such that the user is unable to interact with the iframe's contents (i.e. scrolling, selecting, dragging, etc)? For example, a Google map in an iframe can normally be dragged and zoomed. How would these actions be prevented?

如何使用 Javascript 禁用 iframe,从而使用户无法与 iframe 的内容进行交互(即滚动、选择、拖动等)?例如,通常可以拖动和缩放 iframe 中的 Google 地图。如何防止这些行为?

回答by Simon M

If you're wanting to disable mouse interaction, this should work.

如果您想禁用鼠标交互,这应该可行。

Put a DIV over the top of your iframe, like so (replace top, left, width and height values with your own):

将 DIV 放在 iframe 的顶部,如下所示(将顶部、左侧、宽度和高度值替换为您自己的值):

<div id="blank" style="display:none; position:absolute; top:100px; left:100px; width:600px; height:400px;">

and when you want to disable the iframe, do this in javascript:

当您想禁用 iframe 时,请在 javascript 中执行此操作:

document.getElementById("blank").style.display="block";

回答by alex

I don't know if you can use the disabledproperty like input elements, but you could disable mouse interaction by placing an element over the iframe, though this doesn't stop a user from tabbing to it.

我不知道您是否可以disabled像输入元素一样使用该属性,但是您可以通过在 上放置一个元素来禁用鼠标交互iframe,尽管这不会阻止用户使用 Tab 键访问它。

Probably the best bet is to set the srcattribute to blank, but this will make the iframeblank, not blurred.

可能最好的办法是将src属性设置为空白,但这会使iframe空白,而不是模糊。