我有一个我正在处理的家谱,代码来自https://jsfiddle.net/j5ffw1L7/。这是标记
Parent
如何才能显示只显示Parent
,点击Parent
链接时会显示其他链接?
例如,如果我点击Child
,则会显示两个GrandCHild
。如果我点击左侧的“儿童”,系统会显示Child
,但如果点击右侧的Grandchild
,则会显示左侧“儿童”的<html>
<head>
<title>DisciplinasMatriculadas</title>
<script type="text/javascript" src="index.js"></script>
</head>
<BODY style="font-family: sans-serif">
<fieldset>
<legend>Matricula de aluno em disciplina</legend>
<form action="/myform" method="GET">
ID Aluno: <input type="text" name="aluno" id="aluno">
<br/>
ID Disciplina: <input type="text" name="disc" id="disc">
<br/>
</form>
<input type="button" value="Add" onclick="addDisciplina();alert('Done.');">
</fieldset>
</body>
</html>
将隐藏,等等。
答案 0 :(得分:1)
试试这个:
<head>
<title></title>
<style type="text/css">
.hide {
display: none;
}
.show {
display: block;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("a").click(function () {
$("a:contains('Child')").siblings("ul").removeClass();
$("a:contains('Child')").siblings("ul").addClass("hide");
var ul = $(this).siblings("ul");
$(ul).toggleClass("hide", "show");
});
});
</script>
</head>
<body>
<div class="tree">
<ul>
<li>
<a href="#">Parent</a>
<ul class="hide">
<li>
<a href="#">Child</a>
<ul class="hide">
<li>
<a href="#">Grand Child</a>
</li>
</ul>
</li>
<li>
<a href="#">Child</a>
<ul class="hide">
<li><a href="#">Grand Child</a></li>
<li>
<a href="#">Grand Child</a>
<ul class="hide">
<li>
<a href="#">Great Grand Child</a>
</li>
<li>
<a href="#">Great Grand Child</a>
</li>
<li>
<a href="#">Great Grand Child</a>
</li>
</ul>
</li>
<li><a href="#">Grand Child</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</body>
答案 1 :(得分:1)
默认隐藏li
:
.tree li {
...
display: none;
}
通过添加类(active
)来显示根节点:
.tree li.active {
display: block;
}
单击节点时,将active
类切换到下一个ul
元素中的任何子节点:
$('a').on('click',function() {
// First remove added class incase this node has been activated previously
$(this).next('ul').find('li').removeClass('active');
$(this).next('ul').find('>li').addClass('active');
})
工作演示:
$('a').on('click',function(){
// First remove added class incase this node has been activated previously
$(this).next('ul').find('li').removeClass('active');
$(this).next('ul').find('>li').addClass('active');
})
&#13;
/*Now the CSS*/
* {margin: 0; padding: 0;}
.tree ul {
padding-top: 20px; position: relative;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.tree li {
float: left; text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
display: none;
}
.tree li.active {
display: block;
}
/*We will use ::before and ::after to draw the connectors*/
.tree li::before, .tree li::after{
content: '';
position: absolute; top: 0; right: 50%;
border-top: 1px solid #ccc;
width: 50%; height: 20px;
}
.tree li::after{
right: auto; left: 50%;
border-left: 1px solid #ccc;
}
/*We need to remove left-right connectors from elements without
any siblings*/
.tree li:only-child::after, .tree li:only-child::before {
display: none;
}
/*Remove space from the top of single children*/
.tree li:only-child{ padding-top: 0;}
/*Remove left connector from first child and
right connector from last child*/
.tree li:first-child::before, .tree li:last-child::after{
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.tree li:last-child::before{
border-right: 1px solid #ccc;
border-radius: 0 5px 0 0;
-webkit-border-radius: 0 5px 0 0;
-moz-border-radius: 0 5px 0 0;
}
.tree li:first-child::after{
border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
}
/*Time to add downward connectors from parents*/
.tree ul ul::before{
content: '';
position: absolute; top: 0; left: 50%;
border-left: 1px solid #ccc;
width: 0; height: 20px;
}
.tree li a{
border: 1px solid #ccc;
padding: 5px 10px;
text-decoration: none;
color: #666;
font-family: arial, verdana, tahoma;
font-size: 11px;
display: inline-block;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
/*Time for some hover effects*/
/*We will apply the hover effect the the lineage of the element also*/
.tree li a:hover, .tree li a:hover+ul li a {
background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}
/*Connector styles on hover*/
.tree li a:hover+ul li::after,
.tree li a:hover+ul li::before,
.tree li a:hover+ul::before,
.tree li a:hover+ul ul::before{
border-color: #94a0b4;
}
/*Thats all. I hope you enjoyed it.
Thanks :)*/
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!--
We will create a family tree using just CSS(3)
The markup will be simple nested lists
-->
<div class="tree">
<ul>
<li class="active">
<a href="#">Parent</a>
<ul>
<li>
<a href="#">Child</a>
<ul>
<li>
<a href="#">Grand Child</a>
</li>
</ul>
</li>
<li>
<a href="#">Child</a>
<ul>
<li><a href="#">Grand Child</a></li>
<li>
<a href="#">Grand Child</a>
<ul>
<li>
<a href="#">Great Grand Child</a>
</li>
<li>
<a href="#">Great Grand Child</a>
</li>
<li>
<a href="#">Great Grand Child</a>
</li>
</ul>
</li>
<li><a href="#">Grand Child</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
&#13;
修改强>
要在点击孩子时隐藏兄弟姐妹,您必须追溯到父母的“孩子”。并从任何兄弟姐妹中删除活动类:
$('a').on('click',function() {
// First remove added class incase this node has been activated previously
$(this).next('ul').find('li').removeClass('active');
$(this).closest('li').siblings().removeClass('active');
$(this).next('ul').find('>li').addClass('active');
});
答案 2 :(得分:1)
HTML
<ul id="menu">
<li><a href="#">Parent</a>
<ul>
<li><a href="#">Products</a>
<ul>
<li><a href="#">Pen</a></li>
<li>
<a href="#">Shoes</a>
<ul>
<li>Black</li>
<li>Brown</li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Categories</a>
<ul>
<li>Page</li>
<li>Data</li>
</ul>
</li>
</ul>
CSS
#menu ul {display:none;}
的js
jQuery(document).ready(function(){
jQuery("#menu li a").click(function(e) {
var li = jQuery(this).parent('li');
if(!li.has("ul")) {
return;
}
li.children('ul').toggle();
});
});