jquery对iframe支持不好的解决办法

来源:互联网 发布:高压电缆打压试验数据 编辑:程序博客网 时间:2024/04/30 15:51

再次发现jQuery 对iframe操作支持不好!不过结合javascript的dom可以解决问题

在线演示

demo.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>demo</title>
<script src="http://demo.deeka.cn/limit2checked/jquery-1.2.6.pack.js"></script>
<script>
<!--
$(function(){
   $("#code").click(function(){
     //alert(document.getElementById('ifr').contentWindow.document.body.innerHTML);
     alert($(document.getElementById('ifr').contentWindow.document.body).html());
   });
   $("#btn").click(function(){
     $(document.getElementById('ifr').contentWindow.document.body).html($(this).html());
   });
   $("#img").click(function(){
     $(document.getElementById('ifr').contentWindow.document.body).html("<img src='http://www.lanrentuku.com/images/uppic/20080324175817.jpg' />");
   });
});
-->
</script>
</head>

<body><iframe id="ifr" name="ifr" src="iframe.html" style="width:300px; height:200px; border:#CCCCCC solid 1px;"></iframe>
<p id="btn" style="cursor:pointer">把我写进<b>iframe</b>里面去</p>
<p id="img" style="cursor:pointer">插入图片</p>
<p id="code" style="cursor:pointer">查看源码</p>
</body>
</html>
http://hi.baidu.com/linjk03/blog/item/8a1cef388f2da92fb8998f5a.html

原创粉丝点击