【发布时间】:2014-08-02 14:12:32
【问题描述】:
我想要实现的很简单:当你按下一个按钮时,它会创建一个带有选项的选择元素。我可以很好地执行选择元素,但选项不是很多。我已经尝试了很多东西。这是我使用的代码,还有一些 cmets 可以提供帮助。
<!--This is the element that we append the JavaScript to. The code that achieves the task is at the bottom.-->
<p id="clonePanel"></p>
<script>
//Don't worry about the first pid stuff.
var pid = 0;
function dupPan() {
pid++;
//Here we create a "p" element. The following code is the element's content and specs.
var newPanel = document.createElement("p");
newPanel.id=pid;
//Here we create a "select" element (a drop down list).
var newList = document.createElement("select");
//Here we create a text node.
var newListData = document.createTextNode("Example of option");
//Here we append that text node to our drop down list.
newList.appendChild(newListData);
//Here we append our list to our "p" element.
newPanel.appendChild(newList);
//Finally, we append the "p" element to the document, or the "clonePanel" p element.
document.getElementById("clonePanel").appendChild(newPanel);
}
</script>
希望 cmets 提供帮助。应该发生的是一个选择元素与一个文本节点一起生成。然后将两者附加在一起。最后,所有这些东西都被附加到一个 p 元素上,最终被放入文档中。我知道我做错了什么。
我认为我创建文本节点的方法不正确。我觉得还有别的。如果您知道答案,请告诉我正确的代码行吗?那很好啊。是的,我已经查看了可以找到的任何帮助资源,但无济于事。
感谢您阅读并帮助我!
【问题讨论】:
-
您不能将文本节点附加到选择元素,您必须创建一个选项元素,将文本附加到该元素,然后将选项附加到选择。
标签: javascript code-generation