gpt4 book ai didi

jquery - 子菜单点击事件不起作用/触发

转载 作者:行者123 更新时间:2023-12-01 05:53:43 30 4
gpt4 key购买 nike

主菜单的点击代码适用于类更改,但不适用于子菜单...当单击食物或鞋子等子菜单项时,它不会触发警报命令...事实上,悬停非常适合子菜单但不是活跃的

HTML

<aside>
<div class=sidebar>
<ul class=nav>
<li class="home active" data-class="page_home">HOME</li>
<li class="products" data-class="page_products">BYE</li>
<li class="ART" data-class="page_art">ART</li>
</ul>
</div>
</aside>
<article>
<div class="page_home show">
<div class="myhome">
<h1>Home</h1>

<p>this is the index page</p>
</div>
</div>
<div class="page_products">
<ul class="productcat">
<li class="food">FOOD</li>
<li class="shoes">SHOES</li>
<li class="cloths">CLOTHS</li>
</ul>
</div>
<div class="page_art">
<div class="myart">
<h1>ART</h1>

<p>this is art page</p>
</div>
</div>
</article>

JS

$(".page_products ul.productcat li").on('click', function (e) {
var $this = $(this),
// Class of the one to be shown
subclazz = $this.data("class");
alert("button clicked");

$this.addClass('active').siblings().removeClass('active');
}

$("ul.nav li").on('click', function (e) {
var $this = $(this),
// Class of the one to be shown
clazz = $this.data("class");

$this.addClass('active').siblings().removeClass('active');

$('article > div').removeClass('show');
$('.' + clazz).addClass('show');

});

CSS

body {
width:1020px;
margin:0 auto;
position:absolute;
}
aside {
float:left;
display:block;
width:20%;
}
article {
float:right;
display:block;
margin-top:0;
width:75%;
}
.page_home, .page_products, .page_art{
visibility:hidden;
}
.show {
visibility: visible;
}
.sidebar {
margin-left:10px;
}
.nav {
display:block;
margin-top:60px;
list-style-type:none;
}
ul {
list-style-type:none;
color:#000;
}
.nav > li {
background: #666 no-repeat;
width:150px;
height:65px;
color:#000;
}
.nav >li:hover {
background:#06F no-repeat;
}
.nav > li.active {
background:#06F no-repeat;
}


.productcat > li {
background: #666 no-repeat;
width:150px;
height:65px;
color:#fff;
display:inline;
padding:10px;
}


.productcat>li:hover {
background:#06F no-repeat;

}

.productcat > li.active {
background:#06F no-repeat;
}

最佳答案

尝试使用

$(".page_products ul.productcat li").on('click', function (e) {
var $this = $(this);
// Class of the one to be shown
subclazz = $this.attr("class");
alert("button clicked");

$this.addClass('active').siblings().removeClass('active');
}

我改变了

subclazz = $this.data("class");

subclazz = $this.attr("class");

var $this = $(this),

var $this = $(this);

关于jquery - 子菜单点击事件不起作用/触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18378782/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com