【发布时间】:2014-05-15 09:12:12
【问题描述】:
这是我的javascript
<head runat="server">
<script type="text/javascript" language="javascript">
function createQuestionPanel() {
var element = document.createElement("Input");
element.setAttribute("type", "button");
element.setAttribute("value", "button");
element.setAttribute("name", "button");
var div = '<div>top div</div>';
div[0].appendChild(element);
}
function formvalidate() {
}
</script>
</head>
<body onload="createQuestionPanel()">
</body>
</html>
它抛出错误 "AppendChild is not a function" 。我试图寻找解决方案..有人建议在
的地方div.appendChild(元素);
这应该发布
div[0].appendChild(元素);
它没有改变错误。请推荐
【问题讨论】:
-
该值是一个字符串,而不是一个 DOM 元素。字符串没有
appendChild()方法。查看您创建<input>元素的方式,您将需要类似<div>的内容。 -
就像你创建 input 元素一样,创建一个这样的 div 元素让 appendChild 工作
-
您应该将元素附加到元素。创建具有“top div” ID 的 div 元素和:document.getElementById("top div").appendChild(element);
标签: javascript html .net