簡體   English   中英

在 Javascript 中查找元素

[英]Finding elements in Javascript

我剛剛開始編寫擴展程序,但遇到了問題。 我正在使用“getElementById”來查看這是否是正確的網頁。 而且我總是不管什么相同的結果“null”。 有人能幫助我嗎? 這是代碼:

var abc = document.getElementById('questionText');
console.log(abc);
if (abc == null) {
    alert("not working");
} else {
    alert("works");
} 

將代碼包裝在 DOMReady 中,否則您可能最終會在 HTML 文檔完全加載和解析之前尋找元素。

document.addEventListener('DOMContentLoaded', function() {
  var abc = document.getElementById('questionText');
  console.log(abc);
  if (abc == null) {
    alert("not working");
  } else {
    alert("works");
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM