开发者

Javascript: document.getElementById() returns NULL

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

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


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

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


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

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

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.

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


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

Get rid of that bogus SVG doctype too.


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

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

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


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

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

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.


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

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

removing it solved my problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜