javascript 获取 INVALID_CHARACTER_ERR:DOM 异常 5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11704320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting INVALID_CHARACTER_ERR: DOM Exception 5
提问by Rafael Adel
I'm writing a simple to-do list. that a user input a text and the it's added as a checkbox
. But i'm getting this error i have no idea what's it about
我正在写一个简单的待办事项清单。用户输入文本并将其添加为checkbox
. 但是我收到这个错误我不知道这是怎么回事
INVALID_CHARACTER_ERR: DOM Exception 5
INVALID_CHARACTER_ERR: DOM Exception 5
window.onload = function(){
var textBox = document.getElementById("taskInput"),
submitBtn = document.getElementById("submit"),
taskPool = document.getElementById("todoTask");
submitBtn.addEventListener("click", function(){
var task = document.createElement("<input type=\"checkbox\">" + textBox.value + "</input>");
taskPool.appendChild(task);
});
}
回答by Musa
document.createElementtakes the tag name only as its parameter, you'll have to set the type and value after
document.createElement仅将标签名称作为其参数,您必须在之后设置类型和值
var task = document.createElement("input")
task.type = "checkbox";
task.value = textBox.value;
Also input tags are empty, there are no closing tag or inner html, the value is set as an attribute in markup.
输入标签也为空,没有结束标签或内部 html,该值设置为标记中的属性。