我有一个显示嵌入式 YouTube 视频的 iFrame。
<iframe width="560" height="315" src="http://www.youtube.com/embed/xxx" frameborder="0" allowfullscreen></iframe>
Run Code Online (Sandbox Code Playgroud)
我想要做的是在用户点击 You Tube 视频时以某种方式捕获点击事件,以便我可以调用http://myserver.com/dostuff rest api 来更新跟踪点击次数和点击次数的外部服务器计数器当然允许视频按预期播放。
任何信息将不胜感激。
我有一个包含 iframe 的组件,我想在 React 中访问其内容,我使用 ref 来处理 iframe,如何从 iframe 获取所有锚点标签
这是我的代码:
const GridGenerator = () => {
const [loading, setLoading] = useState(true);
const gridIframe = useRef(null);
function handleIframe() {
setLoading(false);
const iframeItem = gridIframe.current;
const anchors = iframeItem.contentWindow.getElementsByTagName("a");
}
return (
<div>
{loading ? <div className={styles.loader} /> : null}
<iframe
title="Grid Generator"
ref={gridIframe}
src="https://collectearth.users.earthengine.app/view/collect-earth-grid-generator"
width="100%"
height="1000px"
frameBorder={0}
onLoad={handleIframe}
/>
<Link to={routes.HOME}>Go Back</Link>
</div>
);
};
Run Code Online (Sandbox Code Playgroud)
所以,它运行良好,直到:
const iframeItem = gridIframe.current;
Run Code Online (Sandbox Code Playgroud)
它返回 iframe 标签,但是这条线不能很好地工作
const anchors = iframeItem.contentWindow.getElementsByTagName("a");
Run Code Online (Sandbox Code Playgroud)
有什么解决办法吗?谢谢
这是我的 iframe:
#htmlDiv {
position: absolute;
top: 0px;
left: 300px;
height: 650px;
width: 1130px;
}
#htmlFrame {
position: absolute;
top: 0px;
left: 0px;
margin: 10px 10px 10px 10px;
padding: 0px 0px 0px 0px;
height: 630px;
width: 1110px;
}
<div id="htmlDiv">
<iframe id="htmlFrame"></iframe>
</div>
$('#htmlFrame').click(function(){
alert('clicked');
});
Run Code Online (Sandbox Code Playgroud)
但是当我在 iframe 内单击时,它不会显示“已单击”。
如何检测 iframe 内的点击。
我有一个包含iframe的页面.
<div id="my_content">
<iframe id="my_iframe" src="http://mysite.com/iframe.html">
</div>
Run Code Online (Sandbox Code Playgroud)
我需要拦截iframe内的点击.我试过这个代码,但它不起作用:
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#my_iframe').click(function(){
alert('click inside iframe');
});
});
Run Code Online (Sandbox Code Playgroud) 当有人点击链接时,我正在使用iframe在主页上运行html文件.当我将html文件单独用作网页时,可以访问多个关键抑制和此类工作等内容.但是当我通过iframe访问html时,它们不是.这甚至可能吗?
编辑
哦,我在我的main.html文件中有一个功能,可以检测按键按下并根据按键播放视频文件(这是一个虚假的视频游戏).它使用eventlisteners和对象来检测键的位置.但是,当我在其他html页面index.html的iframe中查看它时,这不起作用
我正在尝试点击iframe时点击功能,但它不会触发.有可能吗?
$("#iframe").click(function() {
alert("works");
});
<iframe id="iframe" src="http://site.com" scrolling="no"></iframe>
Run Code Online (Sandbox Code Playgroud)