getElementbyID 无法访问样式属性来读取或设置它
我有一些简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无点 - ID 是参数,而不是方法:
No dot - the ID is a parameter, not a method: