gpt4 book ai didi

javascript - jQuery:检查一个对象是否有类

转载 作者:太空狗 更新时间:2023-10-29 15:21:19 25 4
gpt4 key购买 nike

试图检查一个对象是否有一个类。看起来很简单,但我无法让它工作。这是我的代码:

Javascript

$('ul.nav li').click(function(){    
if $(this).hasClass('selected') {
alert('This is selected!');
}

else {
alert('This is not selected!');
}
});

$('ul.nav li:first-child').addClass('selected');

HTML

<ul class="nav">
<li>Who we work for</li>
<li>Articles and interviews</li>
<li>Job openings</li>
<li>What the #%!$@ is Post Typography?</li>
</ul>

<ul class="content">
<li>This is who we work for.</li>
<li>These are articles and interviews.</li>
<li>These are our job openings.</li>
<li>This is some info about Post Typography.</li>
</ul>

最佳答案

if $(this).hasClass('selected') {

应该是

if($(this).hasClass('selected')){

当您查看浏览器的错误控制台时,很容易观察到这一点。 :-)

关于javascript - jQuery:检查一个对象是否有类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10657043/

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