问题
<script>
$(document).ready(function(){
var xml = "<root> \
<method name='A'> \
<childcall name='B'></childcall> \
<childcall name='C'></childcall> \
</method> \
<method name='B'> \
<childcall name='D'></childcall> \
</method> \
<method name='C'> \
<childcall name='D'></childcall> \
<childcall name='E'></childcall> \
</method> \
</root>";
var data = $.parseXML(xml);
console.log(data);
$(data).find('method').each(function(){
var name = $(this).attr('name');
$('<div class="items"></div>').html('<a href="'+name+'">'+name+'</a>').appendTo('#page-wrap');
$(this).children('childcall').each(function(){
name = $(this).attr('name');
$('<div class="items"></div>').html('<a href="'+name+'">'+name+'</a>').appendTo('#page-wrap');
});
});
});
</script>
<body>
<div id="page-wrap"></div>
</body>
The above code traverses the xml and prints items as - A B C B D C D E. I want to make this a collapsible list, like in the given link: http://www.sendesignz.com/index.php/jquery/77-how-to-create-expand-and-collapse-list-item-using-jquery
Any hint on how to make it collapsible?
EDIT: Thanks for help. Sorry I cannot accept more than one answer as correct. So Shikiryu solution is also correct.
回答1:
If you need to print exactly like given in the link
edit your js code to out put html like this
<ul> <li class="category">Menu 1 <ul> <li>Menu 1 sub 1</li> <li>Menu 2 sub 2</li> </ul> </li> <li>Menu 2</li> </ul>
Us the provided JS code
$('li.category').addClass('plusimageapply'); $('li.category').children().addClass('selectedimage'); $('li.category').children().hide(); $('li.category').each( function(column) { $(this).click(function(event){ if (this == event.target) { if($(this).is('.plusimageapply')) { $(this).children().show(); $(this).removeClass('plusimageapply'); $(this).addClass('minusimageapply'); } else { $(this).children().hide(); $(this).removeClass('minusimageapply'); $(this).addClass('plusimageapply'); } } }); } );
UPDATE1 : Try this JS code it will print the result as I wrote in the point number one *NOTE : the code is not optimized *
$(document).ready(function(){
var xml = "<root> \
<method name='A'> \
<childcall name='B'></childcall> \
<childcall name='C'></childcall> \
</method> \
<method name='B'> \
<childcall name='D'></childcall> \
</method> \
<method name='C'> \
<childcall name='D'></childcall> \
<childcall name='E'></childcall> \
</method> \
</root>";
var data = $.parseXML(xml);
console.log(data);
var htmltxt="test<ul>";
$(data).find('method').each(function(){
var namenode = $(this).attr('name');
var count = 0;
$(this).children('childcall').each(function(){ count++; });
if(count>0){
htmltxt = htmltxt + "<li='category'>" + namenode +"<ul>";
$(this).children('childcall').each(function(){
var name = $(this).attr('name');
htmltxt = htmltxt + "<li>" + name + "</li>";
});
htmltxt = htmltxt + "</ul></li>";
}else{
htmltxt = htmltxt +"<li>"+namenode+"</li>";
}
});
htmltxt = htmltxt + "</ul>";
$("#page-wrap").html(htmltxt);
});
UPDATE 2 JSFiddle
http://jsfiddle.net/faraqsa/CKa6V/
回答2:
First, you need to generate the same HTML as that example (using ul and li instead of div)
$(data).find('method').each(function(){
var hasChild = $(this).children('childcall').length > 0;
curLi += '<li';
curLi += ((hasChild) ? ' class="category plusimageapply">': '>');
curLi += $(this).attr('name');
if(hasChild){
curLi += '<ul>';
$(this).children('childcall').each(function(){
var name = $(this).attr('name');
curLi += '<li><a href="'+name+'">'+name+'</a></li>';
});
curLi += '</ul>';
}
curLi += '</li>';
});
$('#test').append(curLi);
Note that it can be optimized.
Then, you need to indicate some CSS (hide children, adding + and - etc)
.category ul{display:none;}
Finally, you need to apply their JS
$('li.category').click(function(event){
if($(this).is('.plusimageapply')) {
$(this).children().show();
$(this).removeClass('plusimageapply');
$(this).addClass('minusimageapply');
}
else
{
$(this).children().hide();
$(this).removeClass('minusimageapply');
$(this).addClass('plusimageapply');
}
});
That gives : http://jsfiddle.net/dujRe/1/
回答3:
Use JQuery's toggle effect, it goes something like this:
$("#CollapseTrigger").click(function () {
$("#ListToBeHidden").toggle("slow");
});
来源:https://stackoverflow.com/questions/15922108/collapsible-list-using-jquery