Javascript getElementById 返回 null?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8739605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:17:23  来源:igfitidea点击:

getElementById returns null?

javascriptgetelementbyid

提问by Sheena

document.getElementById('id of div that definately exists')returns null.

document.getElementById('id of div that definately exists')返回空值。

I originally loaded the javascript last in order to make sure I wouldn't need to worry about the onload event. I also tried using the onload event. It's very spooky. Any ideas or help would be greatly appreciated.

我最初是最后加载 javascript 的,以确保我不需要担心 onload 事件。我也尝试使用 onload 事件。这是非常可怕的。任何想法或帮助将不胜感激。

回答by cameronroe

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

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

(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.

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

Better option..

更好的选择..

(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);

回答by PanJanek

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

Please, post your code.

请贴出你的代码。

回答by Fabrizio Calderan

There could be many reason why document.getElementByIddoesn't work

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

  • 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 contentdocumentbefore 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

  • 您的 ID 无效

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

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

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

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

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

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