我正在使用iframe加载外部网址。这是结构-
<iframe id="iframe-wrapper" src="http://blog.kpmg.ch/" style="width: 100%; height: 100%; overflow-y: scroll"></iframe>
在iframe内容中滚动时,我正在尝试做一些事情。我尝试了各种方法,没有任何效果。这是我已经尝试过的一些东西-
$(window).load(function(){
$($('#iframe-wrapper').contents()).scroll(function(){
alert("Scrolling");
});
});
$('#iframe-wrapper').load(function(){
$($(this)[0].contentWindow).scroll(function(){
alert("Scrolling");
});
});
3)触发父iframe中的点击事件时,如何使IFRAME作为父项来监听滚动事件
$("#iframe-wrapper").load(function(){
var iframeContent = getFrameTargetElement( document.getElementById("iframe-wrapper") );
iframeContent.onscroll = function(e){
alert("Scrolling");
};
});
$("#iframe-wrapper").load(function(){
$("#iframe-wrapper").contents().find("body").scroll( function(e) {
alert("Scrolling");
});
});
这是行不通的。这是因为有相同来源政策。由于您的页面和iFrame的来源不在同一个域中,因此该事件将永远不会在您的主页上触发。在此处了解更多信息:https :
//developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy
也许您可以使其与以下解决方案之一一起使用:
规避同源政策的方法
因此,对于外部URL(不在同一域中),这总是有问题的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句