我在页面中<iframe name="iniIfm" id="iniIfm"  style="display:none"></iframe>我们平常给document.getElementById("iniIfm").src = decodeURIComponent(arrayUrl.iniUrl);设置iframe的内容如果我有一串HTML代码比如html><body>....</body></html>应该怎么设置呢?

解决方案 »

  1.   

    前提是iframe要和主页面要是同源的
    document.getElementById("iniIfm").contentWindow.document.body.innerHTML = "xxx";
      

  2.   

    对象.src是设置URL文件地址,如果你把字符串加给他,那就乱套了!应该这样:主页面:
    <iframe id="aa" name="aa" src="aa.htm" frameborder="0" scrolling="No" width="300" height="300"></iframe>
    <iframe id="bb" name="bb" src="bb.htm" frameborder="0" scrolling="No" width="300" height="300"></iframe><script>
    window.onload=function(){
        document.getElementById("aa").contentWindow.document.getElementsByTagName('body')[0].innerHTML="<b>woani</b>";
    }
    </script>aa.htm:<!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"/>
    </head>
    <body>
    <div id="wrap">
      <div id="subwrap">
        <div id="content">
          <IMG class=picCore alt="Ceramic Structured Packings" src="1.jpg" style='width:120px;height:120px;vertical-align:top:50%' border=0> 
     
        </div>
      </div>
    </div>
     <input type="button" value="chongzai" onclick="javascript:self.location.reload();">
     </body>
     </html>
    最后结果粗体我爱你将替换aa.htm里的body内容!
      

  3.   

    document.getElementById("iniIfm").innerHTML = 想要的内容
      

  4.   

    window.ifr.document.write("<html><body>这个字符串换成你的字符串</body></html>");
      

  5.   

    刚才没有注意你的iframe的名字window.iniIfm.document.write("<html><body>这个字符串换成你的字符串</body></html>");
      

  6.   

    document.getElementById("iniIfm").contentWindow.document.body.innerHTML = "xxx";
    或者document.getElementById("iniIfm").contentWindow.document.write("<html><body>....</body></html>");