3 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
我想說這是因?yàn)樗鼊?chuàng)建元素的渲染時(shí)間比它后面的邏輯要長。
意思是你的 var element = document.getElementById("txtSearchBelow"); 在瀏覽器識(shí)別該元素之前執(zhí)行。
在調(diào)用 document.getElementById("txtSearchBelow") 之前放入延遲計(jì)時(shí)器

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
它工作得很好,我沒有收到你的抱怨......
document.getElementById("searchResults").innerHTML = '<input id="txtSearchBelow"class="form-control" type="text" placeholder="Search..">'+'<br>';
var inputBelow = document.getElementById("txtSearchBelow");
console.log(inputBelow);
console.log('until here it seems fine.');
var element = document.getElementById("txtSearchBelow");
// If it isn't "undefined" and it isn't "null", then it exists.
if (typeof(element) != 'undefined' && element != null) {
console.log('Element exists!');
// This line works for me...!
document.getElementById("txtSearchBelow").value = 'eyyy please access me';
} else {
console.log('Element does not exist!');
}
<div id="searchResults"></div>
- 3 回答
- 0 關(guān)注
- 216 瀏覽
添加回答
舉報(bào)