[英]Div visibility toggle not working
I'm making a tree structure using html and css. 我正在使用html和css制作树结构。 This is the final structure that I should reach: http://jsfiddle.net/yrE7N/1/ 这是我应该达到的最终结构: http : //jsfiddle.net/yrE7N/1/
What I need is, on clicking a node, its children node will appear. 我需要的是,单击一个节点,将出现其子节点。
I've done this till now: 到目前为止,我已经做到了:
JS Fiddle: http://jsfiddle.net/ZTkLg/11/ JS小提琴: http : //jsfiddle.net/ZTkLg/11/
I've used this JS function 我已经使用了这个JS函数
var _hidediv = null;
function showdiv(id) {
if(_hidediv)
_hidediv();
var div = document.getElementById(id);
div.style.display = 'block';
_hidediv = function () { div.style.display = 'none'; };
}
The thing is, the JS function doesn't seem to be toggling the visibility of the div stage-two
. 问题是,JS函数似乎并未切换div stage-two
的可见性。
I've used this function before on this page: http://leonardorestaurant.in/menu and it worked but I can't figure the problem out in this case. 我以前在此页面上使用过此功能: http : //leonardorestaurant.in/menu ,它可以正常工作,但在这种情况下我无法解决问题。
The console in my browser prints out : 浏览器中的控制台将输出:
Uncaught TypeError: Cannot read property 'style' of null
Which means that here : 这意味着这里:
div.style.display = 'block';
div is not the result you think it should be... That tells us that id is not what we think here : div不是您认为应该的结果...这告诉我们id不是我们在这里的想法:
var div = document.getElementById(id);
Which I confirmed by using : 我通过使用以下方式确认了:
console.log(id);
inside your function. 内部功能。
The id value is actually the <div id="two">
id值实际上是<div id="two">
So, basically you already have the element you're looking for. 因此,基本上您已经有了要查找的元素。
However, you've got bigger problems, which is that you need a toggle function, I'm just guessing. 但是,我遇到了更大的问题,那就是您需要一个切换功能。 Try using this : 尝试使用这个:
function toggleDiv(id) {
var el = document.getElementById(id);
var newDisplayValue = "none";
if ( el.style.display && el.style.display === "none" ) {
newDisplayValue = "block";
}
el.style.display = newDisplayValue;
}
and change to this : 并更改为:
<a href=# onclick="toggleDiv('two');">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.