jQuery iframe 内容更改事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2433966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:30:31  来源:igfitidea点击:

iframe contents change event?

jqueryiframe

提问by mcgrailm

How can I detect when the iframe content has changed and do something upon that change pseudo code below

我如何检测 iframe 内容何时发生更改并根据下面的更改伪代码执行某些操作

 $('#waframe').contents().change(function(){
   //do stuff
  });

采纳答案by Pointy

Well, the browser seems to generate a "load" event on an <iframe>element in the context of the containing page. The "load" fires when the "src" is changed; not sure whether it fires when the iframe changes itself.

好吧,浏览器似乎<iframe>在包含页面的上下文中的元素上生成“加载”事件。当“src”改变时“load”触发;不确定它是否会在 iframe 更改自身时触发。

edit:yes it does seem to fire when the page reloads "internally"

编辑:是的,当页面“内部”重新加载时,它似乎确实会触发

Thus you might try:

因此,您可以尝试:

$('iframe#yourId').load(function() {
  alert("the iframe has been loaded");
});

回答by Edson Martins

Am doing a litle trick (dirt but it works) :) to display a loading object

我正在做一个小技巧(肮脏但有效):) 来显示加载对象

function changeDet(old){ //detect src changes
    if($("iframe#ifID").attr('src') != old){
        $("iframe#ifID").width(0); //error if i hide ...?
    }

    setTimeout('changeDet("' + $("#db").attr('src') + '")',10);
}

var df = $('#iframe#ifID');

df.load(function(){ //content loaded
setTimeout('$("#db").width(var_default_width);',100);
});

changeDet();

$(function(){   
    //jq stuffs...
})
</script>

回答by ravi404

You can use this to fire event on change.. Works on IE8 and firefox

您可以使用它在更改时触发事件.. 适用于 IE8 和 firefox

if(document.addEventListener){
    yourIFrameObject.contentWindow.document.addEventListener('keyup', yourFunction, false);
   // window.frames[0].document.addEventListener('keyup', yourFunction, false);
}
else{
//for IE8
    yourIFrameObject.contentWindow.document.attachEvent('onkeyup', yourFunction);
}

You could use a mouseupstatement if necessary

mouseup如有必要,您可以使用声明