我想在表单<form>里面添加新的DOM元素。 使用appendChild()来添加。可是appendChild()在<form>里面就是用不了。但是把appendChild放到<form>外面,立马就可以用了。
请问有什么办法可以让appendChild()在表单里面使用的吗?<!DOCTYPE html>
<html>
<body><form>
<ul id="myList"><li>Coffee</li><li>Tea</li></ul>
<p id="demo">Click the button to append an item to the list</p>
<button onclick="myFunction()">Try it</button>
</form><script>
function myFunction()
{
var node=document.createElement("LI");
var textnode=document.createTextNode("Water");
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);
}
</script><p><strong>Note:</strong><br>First create an LI node,<br> then create a Text node,<br> then append the Text node to the LI node.<br>Finally append the LI node to the list.</p></body>
</html>
HTMLDOMjavascript
请问有什么办法可以让appendChild()在表单里面使用的吗?<!DOCTYPE html>
<html>
<body><form>
<ul id="myList"><li>Coffee</li><li>Tea</li></ul>
<p id="demo">Click the button to append an item to the list</p>
<button onclick="myFunction()">Try it</button>
</form><script>
function myFunction()
{
var node=document.createElement("LI");
var textnode=document.createTextNode("Water");
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);
}
</script><p><strong>Note:</strong><br>First create an LI node,<br> then create a Text node,<br> then append the Text node to the LI node.<br>Finally append the LI node to the list.</p></body>
</html>
HTMLDOMjavascript
解决方案 »
- 关于datapicker控件获得焦点问题.(在线等)
- JS 函数载入不成功,谢谢
- javascript 如何求两个日期差的天数?如:20060701-20060303.
- 我们的JSP做信息录入是能不能做到这样!!!
- 很急:利用javascript把url参数以post方式传到另一个页面
- 还是联动,又是JavaScipt惹祸.
- 多个frame能否操作同一个变量??
- 这一句是什么意思?var dialogWin = new Object() Object是什么对象?
- 请问有没有javascript特效讲解的教程
- 浏览器用JS给INPUT赋值后不能保存,求助谢谢!!
- dequeue和clearQueue的区别分别是什么,作用各是什么?
- 如果10分钟内键盘和鼠标没有操作,则关闭当前IE窗口.如何实现
<html>
<body><form>
<ul id="myList"><li>Coffee</li><li>Tea</li></ul>
<p id="demo">Click the button to append an item to the list</p>
<button onclick="myFunction(event)">Try it</button>
</form><script type="text/javascript">
function myFunction(e){//添加一个参数,这个参数为点击事件的event
if(e.preventDefault){
e.preventDefault();//FF等阻止DOM节点默认行为,这里是提交表单的行为
e.stopPropagation();
}else{
e.cancelBubble = true;//FF等阻止事件冒泡
e.returnValue = false;//IE阻止DOM节点默认行为,这里是提交表单的行为
}
var node=document.createElement("LI");
var textnode=document.createTextNode("Water");
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);
}
</script><p><strong>Note:</strong><br>First create an LI node,<br> then create a Text node,<br> then append the Text node to the LI node.<br>Finally append the LI node to the list.</p></body>
</html>
e.preventDefault();//FF等阻止DOM节点默认行为,这里是提交表单的行为
e.stopPropagation();//FF等阻止事件冒泡
}else{
e.cancelBubble = true;//IE阻止事件冒泡
e.returnValue = false;//IE阻止DOM节点默认行为,这里是提交表单的行为
}
注释弄错了,剪切了没改。。
<button onclick="myFunction()" type="button">Try it</button>