getElementById 返回 null?

发布于 2024-12-25 12:26:05 字数 1486 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

审判长 2025-01-01 12:26:05

还要注意页面上js的执行方式。例如,如果您执行以下操作:

(function(window, document, undefined) {
  
  var foo = document.getElementById("foo");
  
  console.log(foo);

})(window, document, undefined); 

这将返回 null,因为您将在加载文档之前调用该文档。

使用 window.onload 等待 dom 节点加载:

(function(window, document, undefined) {

  // code that should be taken care of right away

  window.onload = init;

  function init(){
    // the code to be called when the dom has loaded
    // #document has its nodes
  }

})(window, document, undefined);

Also be careful how you execute the js on the page. For example if you do something like this:

(function(window, document, undefined) {
  
  var foo = document.getElementById("foo");
  
  console.log(foo);

})(window, document, undefined); 

This will return null because you'd be calling the document before it was loaded.

Use window.onload to wait for the dom nodes to load:

(function(window, document, undefined) {

  // code that should be taken care of right away

  window.onload = init;

  function init(){
    // the code to be called when the dom has loaded
    // #document has its nodes
  }

})(window, document, undefined);
小…红帽 2025-01-01 12:26:05

它可能是由以下原因引起的:

  1. 无效的 HTML 语法(某些标记未关闭或类似错误)
  2. 重复的 ID - 有两个具有相同 ID 的 HTML DOM 元素
  3. 也许您尝试通过 ID 获取的元素是动态创建的(由 ajax 加载或创建)通过脚本)?

请发布您的代码。

It can be caused by:

  1. Invalid HTML syntax (some tag is not closed or similar error)
  2. Duplicate IDs - there are two HTML DOM elements with the same ID
  3. Maybe element you are trying to get by ID is created dynamically (loaded by ajax or created by script)?

Please, post your code.

毁我热情 2025-01-01 12:26:05

document.getElementById 不起作用的原因可能有很多

  • 您的 ID 无效

    <块引用>

    ID 和 NAME 标记必须以字母 ([A-Za-z]) 开头,后面可以跟任意数量的字母、数字 ([0-9])、连字符 ("-")、下划线 (" _”)、冒号(“:”)和句点(“.”)。
    (资源:什么是 id 属性的有效值HTML?)

  • 你使用了一些已经在标题中用作 名称的 id(例如版权、作者...),它看起来很奇怪,但发生在我身上: 如果你是用IE看一下
    (资源:http://www.phpied.com/getelementbyid-description-in-ie /)

  • 您正在定位框架或 iframe 内的元素。在这种情况下,如果 iframe 加载父级同一域内的页面,您应该在查找元素之前定位 contentdocument
    (资源:在框架内调用特定id )

  • 当节点没有有效地加载到 DOM 中时,您只是在查找一个元素,或者可能是一个简单的拼写错误

我怀疑您使用相同的 ID 两次或更多次:在这种情况下 document.getElementById 应该至少返回第一个元素

There could be many reason why document.getElementById doesn't work

  • You have an invalid ID

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
    (resource: What are valid values for the id attribute in HTML?)

  • you used some id that you already used as <meta> name in your header (e.g. copyright, author... ) it looks weird but happened to me: if your 're using IE take a look at
    (resource: http://www.phpied.com/getelementbyid-description-in-ie/)

  • you're targeting an element inside a frame or iframe. In this case if the iframe loads a page within the same domain of the parent you should target the contentdocument before looking for the element
    (resource: Calling a specific id inside a frame)

  • you're simply looking to an element when the node is not effectively loaded in the DOM, or maybe it's a simple misspelling

I doubt you used same ID twice or more: in that case document.getElementById should return at least the first element

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文