gpt4 book ai didi

javascript - jQuery .addClass 未在 $(this) 上执行

转载 作者:行者123 更新时间:2023-11-28 11:10:21 25 4
gpt4 key购买 nike

我试图在用户单击时更改列表项,将选定的类更改为单击的项目。我有这个代码:

$(function() {
$("a.product-page li").click(function(){
$(this).parent("a").addClass("selected-page");
$("#options a.selected-page").removeClass("selected-page");
});
});

然而,removeClass 可以工作,而 addClass 则不能。

这是Site .

HTML:

<ul id="options">
<a href="#summary" id="summary" class="product-page selected-page"><span></span><li>Summary</li></a>
<a href="#specs" id="specs" class="product-page"><span></span><li>Specs</li></a>
<a href="#media" id="media" class="product-page"><span></span><li>Media</li></a>
<a href="#reviews" id="review" class="product-page"><span></span><li>Reviews</li></a>
</ul>

CSS:

ul#options {
margin-left:0px;
}

ul#options li{
list-style:none;
padding: 10px 20px;
background: #CCC;
border-top: 1px #999 solid;
border-left: 1px #999 solid;
border-right: 1px #999 solid;
}

ul#options a{
display:block;
border:none !important;
}

ul#options a.selected-page span{
position:absolute;
width:0px;
height:0px;
border-width:10px;
border-color:transparent #999999 transparent transparent;
border-style:solid;
margin-left:270px;
margin-top:11px;
}

ul#options a:last-child li {
border-bottom:1px #999 solid;
}

最佳答案

这是因为您要立即删除该类(class)。将这两行翻转过来,就可以了。

$(function() {
$("a.product-page li").click(function(){
$("#options a.selected-page").removeClass("selected-page");
$(this).parent("a").addClass("selected-page");
});
});

正如上面评论中所指出的,您的 HTML 格式也非常错误。这需要修复。

关于javascript - jQuery .addClass 未在 $(this) 上执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12680763/

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