您好!很高兴为您答疑!
可以的,请参考下面这段代码:
<!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(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>
您可以在火狐社区了解更多内容。希望我的回答对您有所帮助,如有疑问,欢迎继续在本平台咨询。
温馨提示:答案为网友推荐,仅供参考