gpt4 book ai didi

javascript - 删除当前的 css 类并使用 jquery 添加新类

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:32 25 4
gpt4 key购买 nike

如何移除当前添加到div的类,并根据条件集添加新类?

我的 css 包含 3 个不同的类:

.red   { background:red }

.blue { background:blue }

.green { background:green }

当时我只想根据 jquery 变量 my_color 设置的条件向 div id #box 添加一个类。my_color 有3值血,花园,天空当时只有一套。

j查询:

<script type="text/javascript">  
(document).ready(function() {

if(my_color == "blood")
{ /*help me to remove current class and add .red */}
else if(my_color == "garden")
{/*help me to remove current class and add .green */ }
else if(my_color == "sky")
{/*help me to remove current class and add .blue */ }

});
</script>

最佳答案

这用红色/绿色/蓝色替换了框上的类

   if(my_color == "blood")
{ $('#box').attr('class','red');}
else if(my_color == "garden")
{ $('#box').attr('class','green');}
else if(my_color == "sky")
{ $('#box').attr('class','blue');}

关于javascript - 删除当前的 css 类并使用 jquery 添加新类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14684089/

25 4 0
文章推荐: css - 打散大字/防止打断布局/Hyphenator js
文章推荐: python - 如何检查 元素是否存在于
文章推荐: java - SASS转CSS编译库
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com