【发布时间】:2019-11-04 22:32:31
【问题描述】:
我刚刚想出了如何检测跨域 iframe 上的点击事件,但它仅适用于桌面,以下代码在检测 iframe 内的点击事件时有效,但是,我还需要它在移动设备上工作,我尝试使用touchstart和touchendevents 为该脚本添加移动支持,但它不起作用。
//Google ADs track conversion
$( document ).ready(function() {
var iframeMouseOver = false;
var iframeTouched = false;
$("#wh-widget-send-button")
.off("mouseover.iframe").on("mouseover.iframe", function() {
iframeMouseOver = true;
})
.off("mouseout.iframe").on("mouseout.iframe", function() {
iframeMouseOver = false;
});
//Add mobile support to this script
$("#wh-widget-send-button")
.off("touchstart").on("touchstart", function() {
iframeTouched = true;
})
.off("touchend").on("touchend", function() {
iframeTouched = false;
});
$(window).off("blur.iframe").on("blur.iframe", function() {
if(iframeMouseOver || iframeTouched){
console.log("Iframe Clicked");
gtag_report_conversion();
}
});
});
更新
请求的HTML,它只是一个div内的简单iframe,还清除了上面的代码以专注于重要部分:
<div id="wh-widget-send-button">
<iframe src="http://anyexternaldomain.com"></iframe>
</div>
【问题讨论】:
-
我也可以包含 HTML 吗?
-
如果你愿意,你可以用你的答案做一个 jsfiddle
-
对不起。我的意思是你能包含 HTML 吗?
-
@KalimahApps 再次完成检查
-
谢谢。我不确定您需要与 iframe 进行多少交互。只是点击 iframe 还是需要与 iframe 中的某些元素进行交互?
标签: javascript jquery iframe touch-event