更改iframe内容jquery [英] change iframe content jquery
本文介绍了更改iframe内容jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改我的iframe内容,但我不明白
< html>
< head>
< title> test< / title>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js>< / script>
< / head>
< body>
< iframe id =framewidth =100%height =95%src =http://google.com>< / iframe>
< script type = text / javascript>
$('#frame')。contents()。find(body).html('< p> hi< / p>)
< / script>
< / body>
< / html>
有了这段代码,我总是有我的谷歌网页,而不是一个ifram与嗨
解决方案
< script type =text / javascript> ('load',function(){
$(this).contents()。
$(document).ready(function(){
$('#frame')。 find(body).html('< p> hi< / p>);
});
});
< / script>
虽然这是正确的,但当您尝试修改外部主体时,内容永远不会改变资源。由于跨网站脚本规则,您无法这样做。
编辑:
唯一的方法是执行@ user1153551所做的并替换整个文档。
I'd like to change my iframe content but I don't understand how
<html>
<head>
<title>test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<iframe id="frame" width="100%" height="95%" src="http://google.com"></iframe>
<script type=text/javascript >
$('#frame').contents().find( "body" ).html('<p>hi</p>')
</script>
</body>
</html>
With this code I always have my google page and not a ifram with hi
解决方案
<script type="text/javascript">
$(document).ready(function(){
$('#frame').on('load', function() {
$(this).contents().find( "body" ).html('<p>hi</p>');
});
});
</script>
While this is right, the content will never change as you are trying to modify the body of an external resource. You cannot do this due to cross-site scripting rules.
EDIT:
The only way to do it is to do what @user1153551 did and replace the whole document.
这篇关于更改iframe内容jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文