-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathopenClose.js
More file actions
38 lines (38 loc) · 1.43 KB
/
openClose.js
File metadata and controls
38 lines (38 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(function(){
function showIt(href) {
console.log(href);
Array.prototype.slice.call(document.getElementsByClassName("answer")).
forEach(function(div) {
var node=div.parentNode;
if (node.children[0].name==href) node.classList.add("show");
});
return true;
}
var href=document.location.href,hash=href.indexOf('#')+1;
if (hash>0) {
showIt(href.substring(hash));
href=href.substring(0,hash);
} else {href=href+"#";hash=href.length}
Array.prototype.slice.call(document.getElementsByTagName("a")).
forEach(function(a) {
if (a.href.substring(0,hash)==href) {
console.log(a.href);
a.addEventListener("click",function(e){
showIt(a.href.substring(hash));
});
}});
Array.prototype.slice.call(document.getElementsByClassName("openClose")).
forEach(function(ul) {
ul.addEventListener("click",function(e){
e.stopPropagation();
if (e.target.tagName=='LI') {
e.preventDefault();
e.target.classList.toggle("show");
} else if (e.target.parentNode.tagName=='LI') {
e.preventDefault();
e.target.parentNode.classList.toggle("show");
}
return true;
});
});
})();