简体   繁体   English

如果ul li类处于活动状态,则jQuery将一个类替换为另一个类

[英]jQuery replace one class with another if ul li class is active

I'm making a form builder, I would like to change the appearance, for example color of the contents. 我正在制作一个表单构建器,我想改变外观,例如内容的颜色。 When the class equal to active should get a white color of the text but when the rest are not active the text should be black instead. 当等于活动的类应该获得文本的白色但是当其余的不活动时,文本应该是黑色的。

How can I do this with generated 2 class? 如何用生成的2类做到这一点?

Anyway I found something on this forum but it seems not working for me : 无论如何,我在这个论坛上找到了一些东西,但似乎对我不起作用:

  $('.game-star').addClass('game-star2').removeClass('game-star'); 
 .game-star ul li h3 { font-size:14px; color:#fff; line-height:24px; float:left; font-weight:100; margin-top:8px; } .game-star2 ul li h3 { font-size:14px; color:#fff; line-height:24px; float:left; font-weight:100; margin-top:8px; } 
 <div class="game-star" style="height: 198px; overflow: hidden;"> <ul> <li class="Active"> <div class="game-star-icon"></div> <!-- <img src="../images/sideGameMenu1.png"/ class="winner-nameMoolar" "> --> <h3 class="winner-name">Major Millions<br> RMB 1000.00</h3> <!-- <p>RMB 625.78</p> --> </li> <li> <div class="game-star-icon"></div> <h3 class="winner-name">Major Moolah<br> RMB 3,266.41</h3> <!-- <p></p> --> </li> <li> <div class="game-star-icon"></div> <h3 class="winner-name">Major Moolah Isis<br> RMB 4,982.78</h3> <!-- <p></p> --> </li> <li> <div class="game-star-icon"></div> <h3 class="winner-name">发大财<br> RMB 8,888.88</h3> <!-- <p>RMB 396.42</p> --> </li> <li> <div class="game-star-icon"></div> <h3 class="winner-name">我发我发我发发发<br> RMB 9,999.99</h3> <!-- <p>RMB 28.89</p> --> </li> </ul> </div> my CSS : 

Instead of using two different CSS classes, you can use one and utilize the CSS Attribute Selector . 您可以使用一个并使用CSS属性选择器 ,而不是使用两个不同的CSS类。 This selector is applied to the CSS selector you want it to be used on, for example .game-star ul li[class=Active] . 此选择器应用于您希望它使用的CSS选择器,例如.game-star ul li[class=Active] It tell the CSS to look for an element with the name of game-star that has a child node of ul and a ul that has a child node of li WITH the attribute of class=Active . 它告诉CSS来寻找与名称的元素game-star拥有的子节点ulul拥有的子节点li 属性class=Active It comes in handy to simplify stuff that had to be done with JavaScript. 它可以很方便地简化必须使用JavaScript完成的工作。

So instead of managing and changing class names with JavaScript, you only have to add or remove the attribute class=Active on your li. 因此,您不必使用JavaScript管理和更改类名,而只需在li上添加或删除属性class=Active Now, the example I have given you is actually a bad practice example. 现在,我给你的例子实际上是一个不好的例子。 The good practice would be something like this .game-star ul li[data-active=Active] would be more of a correct practice. 好的做法是这样的.game-star ul li[data-active=Active]更像是一种正确的做法。 The reason why is you never want to use a CLASS ATTRIBUTE IN A ATTRIBUTE SELECTOR the reason why? 你之所以不想在一个属性选择器中使用CLASS ATTRIBUTE的原因是什么原因? It because that already built in this way... .game-star ul li.Active You can compound selectors, name, and id together to add more emphasis on your target. 因为它已经以这种方式构建了...... .game-star ul li.Active你可以将选择器,名称和id组合在一起,以增加对目标的强调 You can read more about it here . 你可以在这里阅读更多相关信息。

So my recommendation would be this for your code: 所以我的推荐是你的代码:

HTML HTML

    <div class="game-star" style="height: 198px; overflow: hidden;">

<ul>
    <li data-active="true">
        <div class="game-star-icon"></div>
        <!-- <img src="../images/sideGameMenu1.png"/ class="winner-nameMoolar" "> -->   
        <h3 class="winner-name">Major Millions<br>RMB 1000.00</h3>

        <!-- <p>RMB 625.78</p> -->
    </li>
    <li>
        <div class="game-star-icon"></div> 
        <h3 class="winner-name">Major Moolah<br>RMB 3,266.41</h3>
        <!-- <p></p> -->
    </li>
    <li>
        <div class="game-star-icon"></div>
        <h3 class="winner-name">Major Moolah Isis<br>RMB 4,982.78</h3>
        <!-- <p></p> -->
    </li>
    <li>
        <div class="game-star-icon"></div>
        <h3 class="winner-name">发大财<br>RMB 8,888.88</h3>
        <!-- <p>RMB 396.42</p> -->
    </li>
    <li>
        <div class="game-star-icon"></div>
        <h3 class="winner-name">我发我发我发发发<br>RMB 9,999.99</h3>
        <!-- <p>RMB 28.89</p> -->
    </li>

</ul>
</div>

CSS CSS

.game-star ul li[data-active=true] h3 {
    font-size:14px;
    color:#fff;
    line-height:24px;
    float:left;
    font-weight:100;
    margin-top:8px;
}

EDIT: If you still wish to do it your way, here in a fiddle for yours. 编辑:如果你仍然希望按照自己的方式去做,那么这里就是你的小提琴。 Do remember to change the colors to what you desire! 记得把颜色改成你想要的颜色! :) :)

JSFiddle 的jsfiddle

Change your style to: 将你的风格改为:

.game-star  ul li,.game-star2  ul li {
    font-size:14px;
    color:#000;
    line-height:24px;
    float:left;
    font-weight:100;
    margin-top:8px;
}

.game-star ul li.Active,.game-star2  ul li.Active{
    color:#fff;
}

I updated the code. 我更新了代码。 The classes is equal. 班级是平等的。 Active is white color of the text and the rest are not active the text should be black instead. 活动是文本的白色,其余的不活动,文本应该是黑色的。

.game-star  ul li h3,
.game-star2  ul li h3{
    font-size:14px;
    color:#000;
    line-height:24px;
    float:left;
    font-weight:100;
    margin-top:8px;
}

.game-star  ul li.active h3,
.game-star2  ul li.active h3 {
    color:#fff;
}

http://jsfiddle.net/pyjuk106/2/ http://jsfiddle.net/pyjuk106/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM