Javascript 如何使iframe在其;src";变化?(使用jQuery)
我编写了以下代码:Javascript 如何使iframe在其;src";变化?(使用jQuery),javascript,jquery,iframe,Javascript,Jquery,Iframe,我编写了以下代码: $('#main-content > iframe').load(function(){ var $document = $(this.contentWindow.document); alert($document.height()); $(this).height($document.height()); }); 这在页面首次加载时起作用,但是当用户在使用页面时更改iframe的src时,它不会调整大小 我更改src如下: $('#main
$('#main-content > iframe').load(function(){
var $document = $(this.contentWindow.document);
alert($document.height());
$(this).height($document.height());
});
这在页面首次加载时起作用,但是当用户在使用页面时更改iframe的src
时,它不会调整大小
我更改src如下:
$('#main-content > iframe').attr("src",href);
不必担心XSS-它都是本地的
有什么建议吗?
<script type="text/javascript">
$(document).ready(function(){
$('#main-content > iframe').attr('src','onmouseclick.html');
set();
});
function set() {
$('#main-content > iframe').load(function(){
var $document = $(this.contentWindow.document);
//alert($document.height());
$(this).height($document.height());
});
}
</script>
<div id="main-content">
<iframe id="main-contentiframe"></iframe>
<a href="onmouseclick.html" onmouseover="$('#main-content > iframe').attr('src',this.href);set();return false;">Link</a>
<a href="testscript.html" onmouseover="$('#main-content > iframe').attr('src',this.href);set();return false;">Link</a>
</div>
$(文档).ready(函数(){
$('#main content>iframe').attr('src','onmouseclick.html');
set();
});
函数集(){
$('#main content>iframe').load(函数(){
var$document=$(this.contentWindow.document);
//警报($document.height());
$(this.height($document.height());
});
}
在Internet Explorer中,您可以将eventlistener添加到iFrame的onreadystatechange
,根据我的经验,它比加载
有关更多信息,请访问
示例:
$('#main-content > iframe')[0].onreadystatechange = function(evt)
{
if(window.event)
evt = window.event;
var $document = $(evt.srcElement);
alert($document.height());
$(this).height($document.height());
}
注意,我还没有测试代码,不幸的是,这样做不行。它总是提醒初始高度,从不计算新高度。我编辑了它。这样行吗?我以前做过,我只是想记住我是怎么做的。基本上,在尝试识别高度之前,需要让jQuery等待iFrame完全加载新文件,我只是想找出一种方法来影响iframe,而不是iframe中的当前文档。是的,
attr
函数没有这样的回调。谢谢你把想法放在里面-拿到后写回信?我确实把我编辑的东西用在工作上了。这有用吗?