我有一个仅适用于1级和2级的菜单,但我需要更深入地展示所有子级别。也许最好使用JQuery函数,但我不知道如何! 加载时菜单是这样的:
更新
生成子级别的代码:
foreach ($categories as $category) {
$data['x'] .= '<li><a class=' . $category['category_id'] . ' id=' . $category['category_id'] . ' data-id=' . $category['category_id'] . '>' . $category['name'] . '</a></li>';
}
生成ROOT类别的代码:
<ul id="mnav" style="padding-top: 80px; background-color: #337ab7;">
<?php foreach ($categories as $category) { ?>
<li><a id="<?php echo $category['category_id']; ?>" class="list-group-item active"><?php echo $category['name']; ?></a></li>
<?php } ?>
</ul>
编译PHP - HTML:
<ul id="mnav" style="padding-top: 80px; background-color: #337ab7;">
<li><a id="24" class="list-group-item active">Machinery</a>
<ul class="sub" id="sub1">
<li><a class="2420" id="2420" data-id="2420">Agriculture Machinery Equipment</a></li>
<li><a class="2407" id="2407" data-id="2407">Apparel Textile Machinery</a></li>
<li><a class="2409" id="2409" data-id="2409">Building Material Machinery</a></li>
<div class="list-group"> </div>
</ul>
</li>
<li><a id="29" class="list-group-item active">Packaging & Printing</a></li>
<li><a id="26" class="list-group-item active">Mechanical Parts & Fabrication Services</a></li>
</ul>
为实现这一目标,我正在使用这样一个脚本:
<script type="text/javascript">
$(document).ready(function(){
$('ul#mnav > li > a').on('click', function(e) {
console.log("Level 1");
e.preventDefault();
var cat = $(this).attr('id');
var url = 'index.php?route=test/categoryx/child&category_id=' + cat;
$('#mnav #sub1').remove();
$( $("<ul class='sub' id='sub1'>").load(url + "#myContainer") ).insertAfter( '#mnav #' + cat );
});
$('ul#sub1 a').on('click', function(e) {
console.log("Level 2");
e.preventDefault();
var cat = $(this).attr('id');
var url = 'index.php?route=test/categoryx/child&category_id=' + cat;
$('#sub2').remove();
$( $("<ul class='sub' id='sub2'>").load(url + "#myContainer") ).insertAfter( 'ul#sub1 #' + cat );
});
</script>
我尝试了不同的方式来访问第二级,但它没有成功,我尝试了很多不同的东西。
答案 0 :(得分:0)
您可以尝试:
$(".drop").on("click", "li", function (event) {
console.log('a');
});
或者使用find()函数:
$(".drop").find("li").click(function(){
alert("You clicked on li " + $(this).text());
});