javascript - 如何设置类组合的CSS规则?

标签 javascript jquery html css

我有一个div标签作为

<div class="one two three">foo<div>
<div class="one two four">bar</div>

我正在使用 javascript 设置 css,我可以将样式设置为

.one.two.three{color:red;}
.one.two.four{color:blue;}

但是谁能告诉我如何使用 $(.class).css({"color":"blue"}); 来组合类

最佳答案

  1. 确保您已加载 jQuery,因为您说要使用的 $(selector).css(...) 函数是特定于 jQuery。

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
  2. 设置样式 – 确保等到 DOM 准备就绪,然后再尝试操作样式:

    <script type="text/javascript">
        $(document).ready(function() {
            $(".one.two.four").css({"color":"blue"});
        });
    </script>
    

您可以阅读我在 the jQuery Getting Started guide 中描述的所有内容.

工作演示:http://jsfiddle.net/4b2n97hq/

如果出现问题,open your browser's console并查找有助于解决问题的错误消息。

关于javascript - 如何设置类组合的CSS规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25848726/

相关文章:

javascript - 当用户将鼠标悬停在链接上时想要显示图标/选项

javascript - 如何使用 Javascript/AngularJs 将按钮放置在图像上的特定位置(x,y)

HTML 验证程序 "self-closing syntax"和 "non-void"错误

c# - 无论屏幕尺寸或分辨率如何,定位加载指示器 div

javascript - 从 uglifyjs 源映射中恢复 javascript 源代码

javascript - 通过 Cognito Identity Pool 验证的用户的 MQTT 连接

javascript - 所需的字符串参数不存在 : communicate with fetch

javascript - 使淡入淡出效果在 "Fade Slider"上看起来更漂亮

jQuery:选择和操作 DOM 外部的 html 元素

javascript - 多项选择不突出元素