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

关于javascript 创建控件的问题
我用JS脚本写了下面一段
  var   newNode_usd   =   document.createElement( "iFrame ");
                  newNode_usd.setAttribute( "id ",   val1);
                  newNode_usd.setAttribute( "src ",   " ");
                  newNode_usd.setAttribute( "scrolling ",   "no ");
                  newNode_usd.setAttribute( "frameborder ",   "0 ");
  document.body.appendChild(newNode_usd);
这个是创建一个iframe,如果把这段脚本放在
<form>
<script>
var   newNode_usd   =   document.createElement( "iFrame ");
newNode_usd.setAttribute( "id ",   val1);
newNode_usd.setAttribute( "src ",   " ");
newNode_usd.setAttribute( "scrolling ",   "no ");
newNode_usd.setAttribute( "frameborder ",   "0 ");
document.body.appendChild(newNode_usd);
</script>
</form> 会报错,internet   explore   无法打开http://localhost/test/test.htm
设计发现,如果在这个脚本之前有未必合的 <form>   <div>   <table> 这些时,都会报这个错,如果把这些去掉就不会报错,但是我这段脚本一定要放在这些里面,请问高手们有什么好的办法吗?谢谢了,在线等…………

------解决方案--------------------
//插入DIV
function addDiv(btn)
{
targetUrl = window.prompt( "网址 ", "http://www.baidu.com ");
if(targetUrl == " ")
targetUrl = "http://www.baidu.com ";

btn.insertAdjacentHTML( "beforeEnd ", " <div> <div style=\ "float:right;width:20px;height:20px;\ "> <button onclick=\ "delDiv(this);\ "> X </button> </div> <iframe style=\ "clear:both;width:100%;\ " src=\ " " + targetUrl + "\ "> </iframe> </div> ");
}
//删除DIV
function delDiv(btn)
{
if(confirm( '你确定要删除这个元素? '))
btn.parentElement.parentElement.removeNode(true);
}


-------------
<button id= "btnAddDiv " onclick= "addDiv(this.parentElement); "> 添加 </button>