日期:2014-05-18  浏览次数:20834 次

js中怎么往iframe中添加一个div
<iframe id="showValue" style="width: 400px; height: 95px; border: 0px; solid: #000;"
  src="about:blank"></iframe>
 
<script type="text/javascript">
  window.onload = function () {
  document.getElementById("showValue").contentWindow.document.designMode = "on";
  document.getElementById("showValue").contentWindow.document.contentEditable = true;
  }
</script>
如何往iframe中添加一个div,并且给div赋一个id值

------解决方案--------------------
HTML code

<!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>
    <title></title>
</head>
<body>
<iframe id="showValue" style="width: 400px; height: 400px; border: 0px; solid: #000;" src="about:blank"></iframe>
 
 
<script type="text/javascript">
    window.onload = function () {
        document.getElementById("showValue").contentWindow.document.designMode = "on";
        document.getElementById("showValue").contentWindow.document.contentEditable = true;

        var div = document.createElement("DIV");
        div.id = "divName";
        div.innerHTML = "我在这里了哈";
        div.style.cssText = "border:1px solid #ccc;height:200px;width:200px;color:red;";

        document.getElementById("showValue").contentWindow.document.appendChild(div);
    }
</script>

</body>
</html>