Javascript:document.getElementById() 返回 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6112343/
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-23 20:16:15  来源:igfitidea点击:

Javascript: document.getElementById() returns NULL

javascripthtmlgetelementbyid

提问by pasta

I am quite new with Javascript and I got a problem with document.getElementById() that always returns NULL, and that's driving me nuts.

我对 Javascript 很陌生,我遇到了 document.getElementById() 问题,它总是返回 NULL,这让我很抓狂。

I have a element in my code and I want to get its coordinates so I can move it.

我的代码中有一个元素,我想获取它的坐标以便移动它。

Here is the code:

这是代码:

<html>
  <head>
    <script type="text/javascript" >
      function MoveIt(obj) {
        alert(obj); // returns "Object HTMLDivElement"
        var xx = document.getElementById("arect");

        if(document.getElementById("arect").value == null) {
          alert('NULL >> ' + xx.value);
        }
        else {
          alert('NOT NULL >>' + xx.value);
        }

        posX = xx.style.scrollTop;
        posY = xx.style.left;
      }
    </script>
  </head>

  <body bgcolor="white" >
    <DIV class="background" id="MyDiv2">  
      <div id="arect" name="arect" class="transbox" onmousedown="MoveIt(this);" >
      </div>
    </div>
  </body>
</html>

The above function MoveIt() always returns NULL

上面的函数 MoveIt() 总是返回 NULL

采纳答案by Lightness Races in Orbit

You never checked getElementById(...)for NULL.

您从未检查getElementById(...)NULL.

You checked getElementById(...).valuefor NULL, and divs don't have a "value".

您检查getElementById(...).valueNULL,并且 div 没有“值”。

Also note that you forgot to close that <div />tag, which is illegal in your XHTML... and used an SVG doctype for some reason. SVG is not HTML.

另请注意,您忘记关闭该<div />标签,这在您的 XHTML 中是非法的……并且出于某种原因使用了 SVG 文档类型。SVG 不是 HTML。

It's not really clear what you're trying to do here.

不太清楚你在这里想做什么。

回答by Sailab Rahi

The page contents need to be loaded before trying to read them. Try

页面内容需要在尝试阅读之前加载。尝试

window.onload = function() {
  // run your script in here
}

Or if you're using jQuery, prefer

或者,如果您使用的是 jQuery,则更喜欢

$(document).ready(function() {
  ...
}

回答by Pointy

The "arect" element is a <div>, and <div>elements don't have a "value".

“arect”元素是 a <div><div>元素没有“值”。

Get rid of that bogus SVG doctype too.

摆脱那个虚假的 SVG 文档类型。

回答by MNIK

if(document.getElementById("arect").value == null){
    alert('NULL >> '+ xx.value);
  }

This code always returns null or error. If you want to see if the object exists, do the following....

此代码始终返回 null 或错误。如果要查看对象是否存在,请执行以下操作....

if(xx == null)
   alert('Object does not exist');
else 
   alert('Object exists. Inner HTML: ' + xx.innerHTML);

Also, divdoes not have value. If you want to get the html inside div, use xx.innerHTML

还有,div没有value。如果你想在 div 中获取 html,请使用xx.innerHTML

回答by James McCormack

Firstly, what you're trying to do is fraught with cross-browser inconsistency that would tax a javascript pro, so you will be better off using jQuery if you're new to javascript.

首先,您要做的是充满跨浏览器的不一致,这会给 javascript 专家带来负担,因此,如果您不熟悉 javascript,最好使用 jQuery。

Secondly, xx will not have a value as it is a DIV. You will find that xx is not null in itself.

其次,xx 没有值,因为它是 DIV。你会发现 xx 本身并不为空。

回答by Dogugun Ozkaya

in my case it was because of having this line at the beginning of the jsp/html(whatever) file:

就我而言,这是因为在 jsp/html(whatever) 文件的开头有这一行:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

removing it solved my problem.

删除它解决了我的问题。

回答by Vijender Reddy

if the button is set to visisble= false then you cannot get the id of that button on client side. To hide the button use

如果按钮设置为 vissble=false 则您无法在客户端获取该按钮的 ID。隐藏按钮使用

button1.Style.Add("display","none")-- for visible false

and

button1.Style.Add("display","block")-- for visible true

and even if button is enabled false we cannot get the Id of the button on client side

即使按钮已启用 false 我们也无法在客户端获取按钮的 ID

You can get the id of the button by document.getElementById('<%= button1.ClientID %>'); Or if you set the ClientIDMode="Static" for the control in aspx page you can get it directly by document.getElementById('button1'); Or document.getElementById('MainContent_button1');--- MainContent here is the Id of the contentplaceholder if you have the id of the contenet placeholder different use that id_button1.

可以通过 document.getElementById('<%= button1.ClientID %>'); 获取按钮的id;或者在aspx页面中为控件设置ClientIDMode="Static"可以直接通过document.getElementById('button1'); 或 document.getElementById('MainContent_button1');--- MainContent 这里是 contentplaceholder 的 Id,如果你有不同的使用 id_button1 的内容占位符的 id。