Это мой первый вопрос. Я работаю с деревом Jquery. Я уже создал древовидное представление, и когда я щелкнул родительский узел с помощью мыши, он расширился. Но я действительно хочу расширить каждый родительский узел, нажав на кнопку. Этот код показывает, как я создал древовидную структуру.Jquery treeview-Как развернуть/свернуть данный узел, нажав на кнопку
body, a {
color: #3B4C56;
font-family: sans-serif;
font-size: 14px;
text-decoration: none;
}
a{
\t cursor:pointer;
}
.tree ul {
list-style: none outside none;
}
.tree li a {
line-height: 25px;
}
.tree > ul > li > a {
color: #3B4C56;
display: block;
font-weight: normal;
position: relative;
text-decoration: none;
}
.tree li.parent > a {
padding: 0 0 0 28px;
}
.tree li.parent > a:before {
<!--background-image: url("../images/plus_minus_icons.png"); -->
background-position: 25px center;
content: "";
display: block;
height: 21px;
left: 0;
position: absolute;
top: 2px;
vertical-align: middle;
width: 23px;
}
.tree ul li.active > a:before {
background-position: 0 center;
}
.tree ul li ul {
border-left: 1px solid #D9DADB;
display: none;
margin: 0 0 0 12px;
overflow: hidden;
padding: 0 0 0 25px;
}
.tree ul li ul li {
position: relative;
}
.tree ul li ul li:before {
border-bottom: 1px dashed #E2E2E3;
content: "";
left: -20px;
position: absolute;
top: 12px;
width: 15px;
}
#wrapper {
width: 400px;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Note: IE8 supports the content property only if a !DOCTYPE is specified. -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
\t <link rel="stylesheet" href="treeStyles.css">
\t <script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript" > </script>
\t
\t <script type="text/javascript">
\t $(document).ready(function() {
\t \t \t \t $('.tree li').each(function() {
\t \t \t \t \t \t if($(this).children('ul').length > 0) {
\t \t \t \t \t \t \t \t $(this).addClass('parent');
\t \t \t \t \t \t }
\t \t \t \t });
\t \t \t \t
/* $('.tree li.parent > a').click(function() {
\t \t \t \t \t \t $(this).parent().toggleClass('active');
\t \t \t \t \t \t $(this).parent().children('ul').slideToggle('fast');
\t \t \t \t });*/
\t \t \t \t
\t \t \t \t
$('#button1')
.unbind('click')
.click(function() {
$('.tree li.parent > a').parent().toggleClass('active');
$('.tree li.parent > a').parent().children('ul').slideToggle('fast');
}); \t \t
});
/*
$('#button1').click(function(){
$('#li_1').children('ul').slideToggle('fast');
}),
$('#button2').click(function(){
$('#li_2').children('ul').toggle();
}); */
\t </script>
</head>
<body>
<div id="wrapper">
<div class="tree">
\t \t
<ul id="list">
<div tabindex="-1" id="1">
\t \t <li id="li_1"><a>TestSuite 1</a>
\t \t \t <ul>
<div tabindex="-1" id="11"><li><a>Test 1 1 Started</a></li></div>
<div tabindex="-1" id="12"><li><a>Test 1 1 Passed</a></li></div>
<div tabindex="-1" id="13"><li><a>Test 1 2 Started</a></li></div>
<div tabindex="-1" id="14"><li><a>Test 1 2 Failed</a></li></div>
<div tabindex="-1" id="15"><li><a>Test 1 3 Started</a></li></div>
<div tabindex="-1" id="16"><li><a>Test 1 3 Passed</a></li></div>
\t \t \t </ul>
\t \t </li>
</div>
<div tabindex="-1" id="2">
\t \t <li id="li_2"><a>TestSuite 2</a>
\t \t \t <ul>
<div tabindex="-1" id="21"><li><a>Test 2 1 Started</a></li></div>
<div tabindex="-1" id="22"><li><a>Test 2 1 Passed</a></li></div>
<div tabindex="-1" id="23"><li><a>Test 2 2 Started</a></li></div>
<div tabindex="-1" id="24"><li><a>Test 2 2 Failed</a></li></div>
\t \t \t </ul>
\t \t </li>
</div>
</ul>
</div>
</div>
<br><br>
<input type="button" class="buttonLoad" id="button1" value="Expand TestSuite 1" />
<input type="button" class="buttonLoad" id="button2" value="Expand TestSuite 2" />
<script>
/*
function myLoadFunction1(){
//expand TestSuite1
openMyOwnBranchInTree(li_1);
}
function myLoadFunction2(){
//expand TestSuite2
openMyOwnBranchInTree(li_2);
}
function openMyOwnBranchInTree(idLeaf) {
$('#table' + idLeaf).parents('li').show();
}
*/
</script>
</body>
</html>
Здесь я использовал myLoadFunction1() и myLoadFunction2() функции, чтобы развернуть/свернуть каждый programmatically.Also Я тесты во нужно развернуть/свернуть узел, см его идентификатор.
Я попытался развернуть/свернуть каждый родительский узел с помощью функции openMyOwnBranchInTree(), но он не работает. Как улучшить мой код, чтобы получить мой результат или есть ли простой способ для этой задачи? советы приветствуются .. Спасибо.
Отредактировано.
Я пробовал следующий код. (Отредактируйте предыдущий код и покажите, где это я изменил, комментируя эти строки.) Я прокомментировал функции myLoadFunction1() и myLoadFunction2(), и я добавил код jquery.
Теперь # button1 развернуть/свернуть все дочерние узлы. Но как попасть в мою цель? Может ли кто-нибудь мне помочь? Спасибо.
Спасибо за ответ на мой вопрос, но он не работает .... – halpMe