日期:2014-05-16  浏览次数:20327 次

关于对opener使用appendChild的问题
有二个页面:页面f的内容为:
HTML code

<button onclick="window.open('t.html','','width=800,height=500')">页面</button>
<textarea id="myTextArea" cols="70" rows="20" name="content">网</textarea>



页面t的内容为

HTML code

<script>
var alink=document.createElement("a");
alink.href="www.baidu.com";
alink.innerText="百度";

var oTable = document.createElement("table");
var oTBody = document.createElement("tbody");
var oTR = document.createElement("tr");
var oTD = document.createElement("td");

oTD.appendChild(alink);
oTR.appendChild(oTD);
oTBody.appendChild(oTR);
oTable.appendChild(oTBody);

window.opener.document.getElementById("myTextArea").appendChild(oTable);
</script>




通过f打开t后,window.opener.document.getElementById("myTextArea").appendChild(oTable);
这一句代码不能执行,请问是什么原因,要达到这样的效果应该怎么弄?

------解决方案--------------------
#myTextArea是个多行文本输入框啊,怎么能嵌入别的对象。。你要做的是构造一个含有表格html代码的字符串,然后把这个字符串赋给#myTextArea对象的value属性:

JScript code
var html = '<table><tbody><tr><td><a href="http://www.google.com/">GOOGLE</a></td></tr></tbody></table>';
window.opener.document.getElementById("myTextArea").value = html;