如何禁用iframe元素内部的拖动?

时间:2019-11-02 21:12:22

标签: javascript css iframe drag

我正在尝试禁止在iframe元素内拖动。

例如我的iframe包含一张地图,我希望允许点击地图上显示的项目,但同时,我想防止拖动时地图移动(因此禁用拖动)。

“可拖动”属性无济于事,因为它阻止了iframe本身的拖动。

我也尝试使用iframe的contentDocument和contentWindow属性,但是没有运气。

例如以下代码仅适用于具有空src =“”的框架。 一旦定义了src,就永远不会监听“ click”事件。

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");

1 个答案:

答案 0 :(得分:0)

您是否尝试取消本机事件行为?

var win = iFrame.contentWindow;
var doc = iFrame.contentWindow.document;
doc.addEventListener("drag", function(event){ event.preventDefault(); });