无法修改iframe的内容,出了什么问题? [英] Cannot modify content of iframe, what is wrong?
本文介绍了无法修改iframe的内容,出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图修改iframe的内容,但它不起作用。
这是我的main.html
< html>
< head>< title>主页< / title>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js>< / script>
< / head>
< body>
< h3>主页< / h3>
< iframe src =ifr.htmid =ifrname =ifr>< / iframe>
< / body>
< script>
$(document).ready(function(){
$('#ifr')。load(function(){
$('#ifr')。contents()。find ('body')。html('嘿,我已经更改< body>!Yay !!!'的内容);
});
});
< / script>
< / html>
这是我的ifr.htm
< HTML>
< head>< title> Iframe页面< / title>< / head>
< body>
要在iframe中显示的内容...
< / body>
< / html>
解决方案
请不要忘记跨域
政策,否则将无法正常工作。 现场演示 http://jsfiddle.net/oscarj24/wTWjF/
(只是要知道,我没有使用404页面,看看)
试试这个:
$(document).ready(function(){
$('#ifr')。ready(function(){
$(this).contents()。find('body')。html('嘿,我已经更改< body> Yay!'的内容);
});
});
I am trying to modify content of an iframe but it does not work.
This is my main.html
<html>
<head><title>Main page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<h3>Main page</h3>
<iframe src="ifr.htm" id="ifr" name="ifr"></iframe>
</body>
<script>
$(document).ready(function(){
$('#ifr').load(function(){
$('#ifr').contents().find('body').html('Hey, i`ve changed content of <body>! Yay!!!');
});
});
</script>
</html>
This is my ifr.htm
<html>
<head><title>Iframe page</title></head>
<body>
Content to be displayed in iframe ...
</body>
</html>
解决方案
Please don't forget the cross-domain
policy, otherwise it won't work.
Live demo: http://jsfiddle.net/oscarj24/wTWjF/
(Just to know, I am not using a 404 page, take a look)
Try this:
$(document).ready(function(){
$('#ifr').ready(function(){
$(this).contents().find('body').html('Hey, I have changed content of <body> Yay!');
});
});
这篇关于无法修改iframe的内容,出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文