我在解决这个问题时遇到了一些问题。
第 1 页
<iframe id="block" src="page2.html" tag> </iframe>
我想在 Iframe 中悬停并在悬停的元素具有特定标签时触发事件。
$("*").hover(function(){
var attr = $(this).attr('name');
if (typeof attr !== typeof undefined && attr !== false) {
return; //do something
}
});
我尝试了多种方法,但它们似乎都想要一些不同的东西,我理解从父类触发事件。我似乎不知道该怎么做。我想要的结果:
- 我将 Iframe 悬停在具有
tag
的元素上
- 我在 Iframe 的末尾收到了一个可以进一步处理的响应
我觉得我已经很接近了,但我只需要一小段连接代码,希望有人能解释我需要什么。
最佳答案
只要你在同一个域(你是),就有一个简单的方法:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<iframe id="block" src="page.html" onload="test()"></iframe>
<script>
function test() {
var iframe = document.getElementById('block');
var doc = iframe.contentDocument || iframe.contentWindow.document;
var element = doc.getElementById('hover');
$(element).hover(function() {
element.append("It's working!\n");
});
}
</script>
关于javascript - Iframe 内的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40296308/