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

发布于 2024-11-10 13:12:34 字数 1946 浏览 2 评论 0原文

我有一些简单的 html/css,其中使用 onclick 事件将一些参数发送到 JavaScript。如果其中之一匹配,我就会将 div 的 style.display 属性设置为阻止,以便它作为对链接单击的反应而打开。但我根本无法让 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。它现在只是一个占位符,但仍然应该可见。

这是 Div 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";
       }
     }

I have some simple html/css where I am using an onclick event to send some parameters to a JavaScript. If one of them matches I then set the style.display property of the div to block so that it opens as a reaction to the link click. BUT I cannot get getElementById to work at all. Even a simple alert on the obj itself doesn't work. Below this is the line which is not working:

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

Here is the code.

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

So what it does is turns on the div that you see right above. It's just a placeholder now but it should still go visible.

This is the Divs CSS.

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

}

This is the JavaScript function

    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";
       }
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-11-17 13:12:34

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

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

No dot - the ID is a parameter, not a method:

var displayprop = document.getElementById("dashboardanchor").style.display;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文