我正在尝试禁用在我的 iframe 元素内拖动。
例如我的 iframe 包含一个 map ,我想允许点击 map 中描绘的元素,但同时,我想防止 map 在拖动时移动(因此禁用拖动)。
“可拖动”属性无济于事,因为它会阻止 iframe 本身被拖动。
我还尝试使用 iframe 的 contentDocument 和 contentWindow 属性,但没有成功。
例如以下代码仅适用于具有空 src=""的框架。 一旦我定义了 src,“点击”事件就永远不会被监听。
HTML:
<iframe id="iFrame" width="800" height="600" src="...an embedded map source..."></iframe>
JS:
var win = iFrame.contentWindow;
var doc = iFrame.contentWindow.document;
win.addEventListener("mousedown", function(){
console.log("iframe win clicked");
doc.addEventListener("mousedown", function(){
console.log("iframe content clicked");
最佳答案
您是否尝试过取消 native 事件行为?
var win = iFrame.contentWindow;
var doc = iFrame.contentWindow.document;
doc.addEventListener("drag", function(event){ event.preventDefault(); });
关于javascript - 如何禁用在 iframe 元素内拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58675358/