亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法設置元素的值

無法設置元素的值

偶然的你 2023-12-04 14:40:47
在函數內創建 HTML 輸入元素。調用該函數后,元素顯示在該位置上,我可以輸入它,但無法通過腳本訪問它。經過研究,我添加了一個檢查該元素是否存在,如果存在,我仍然無法在腳本中更改其值...我可以使用 document.getElementById("txtSearchBelow").value = 'access me 更改控制臺中的值'<div id = "searchResults"></div>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){    alert('Element exists!');// this here doesnt work. why not?    document.getElementById("txtSearchBelow").value = 'eyyy please access me'; } else{    alert('Element does not exist!');}我怎么可以通過控制臺更改值,但不能在腳本中更改值?編輯:它的工作有延遲!太感謝了 !
查看完整描述

3 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

我想說這是因為它創建元素的渲染時間比它后面的邏輯要長。

意思是你的 var element = document.getElementById("txtSearchBelow"); 在瀏覽器識別該元素之前執行。

在調用 document.getElementById("txtSearchBelow") 之前放入延遲計時器


查看完整回答
反對 回復 2023-12-04
?
慕的地8271018

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>


查看完整回答
反對 回復 2023-12-04
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

也許是瀏覽器的問題?計算機系統和加載延遲是可能的。我可以運行它



查看完整回答
反對 回復 2023-12-04
  • 3 回答
  • 0 關注
  • 198 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號