[removed] document.getElementById() returns NULL

后端 未结 7 2141
遥遥无期
遥遥无期 2021-01-02 01:23

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

相关标签:
7条回答
  • 2021-01-02 01:31

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

    Get rid of that bogus SVG doctype too.

    0 讨论(0)
  • 2021-01-02 01:31

    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.

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

    0 讨论(0)
  • 2021-01-02 01:36

    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() {
      ...
    }
    
    0 讨论(0)
  • 2021-01-02 01:39

    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.

    0 讨论(0)
  • 2021-01-02 01:42

    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 讨论(0)
  • 2021-01-02 01:44

    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.

    0 讨论(0)
提交回复
热议问题