当我在 html 中定义我的输入标签并通过 id 在 JS 中访问时,我得到了我的标签。
HTML 代码:
<input class="easyui-combobox" name="language" style="width:30%;" id= "XX">
JS代码:
var cc = document.getElementById("XX");
这里一切都很好。
但是当我从 javascript 创建并尝试访问时,我得到了。我想要动态所以我需要从 JS 创建。
JS代码:
var input = document.createElement("input");
input.className = 'easyui-combobox';
input.style = 'width:30%';
input.id = "XX";
这里我在应用这个之后得到了空值:
var cc = document.getElementById("XX");
最佳答案
您必须使用 document.body.appendChild(input);
var input = document.createElement("input");
input.className = 'easyui-combobox';
input.style = 'width:30%';
input.id = "XX";
document.body.appendChild(input);
console.log(document.getElementById("XX"));
关于javascript - document.getElementById 通过 JS 而不是 HTML 给出 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44449748/