3 回答

TA貢獻1796條經驗 獲得超4個贊
我想說這是因為它創建元素的渲染時間比它后面的邏輯要長。
意思是你的 var element = document.getElementById("txtSearchBelow"); 在瀏覽器識別該元素之前執行。
在調用 document.getElementById("txtSearchBelow") 之前放入延遲計時器

TA貢獻1796條經驗 獲得超4個贊
它工作得很好,我沒有收到你的抱怨......
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 關注
- 198 瀏覽
添加回答
舉報