getElementbyID无法访问样式属性来读取或设置它

时间:2011-05-30 04:41:39

标签: properties getelementbyid

我有一些简单的html / css,我使用onclick事件将一些参数发送到JavaScript。如果其中一个匹配我,则将div的style.display属性设置为block,以便它作为对链接点击的反应而打开。但是我无法让getElementById工作。甚至对obj本身的简单警报也行不通。下面是不起作用的行:

var displayprop = document.getElementById。(“dashboardanchor”)。style.display;

这是代码。

    Div nested several levels in other divs

    <div id="mainpagemenuboxcontainer">
        <div class="menulinkboxes" id="dashboarddiv">
        <a class="mainmenulinks" id="dashboardanchor"
        onmouseover='mouseOver("dashboarddiv")'
        onmouseout='mouseOut("dashboarddiv")'
        onclick='openPane("dashboarddiv,dashboardanchor")'
        href="#">Dashboard</a>
        </div>
    </div>

    <div id="mainpagecontainer">
        <div id="dashboardpane">dashboardpane</div>
        <div id="dcsmpane">dcsmpane</div>
        <div id="lgglasspane">lgglasspane</div>
        <div id="siprehashpane">siprehashpane</div>
    </div> <!-- End Mainpagecontainer -->

所以它的作用是打开你上面看到的div。它现在只是一个占位符,但它应该仍然可见。

这是Divs CSS。

    #dashboardpane {
    margin: 10px;
    padding: 5px;        
    font-family: monospace;
    background-color: white;
    border:1px solid blue;
    width: inherit;
    height: 700px;
    display: none;

}

这是JavaScript函数

    function openPane(elements)
     {
      var elements_array = elements.split(",");
      var paneDivId = elements_array[0];
      var paneAnchorId = elements_array[1];

      if (paneAnchorId == "dashboardanchor")
       {
        alert("gets to here");      
        var displayprop = document.getElementById.("dashboardanchor").style.display;
         // will not get to here
         alert (displayprop);
         document.getElementById.("dashboardanchor").style.display = "block";
       }
     }

1 个答案:

答案 0 :(得分:1)

无点 - ID是参数,而不是方法:

var displayprop = document.getElementById("dashboardanchor").style.display;