使用jQuery从CSS文件获取属性

发布于 2025-01-27 05:10:32 字数 123 浏览 5 评论 0原文

我有一个CSS文件。在其中的某个地方,我有一个:

.color-base {color:#f0f0f0; }

我的问题是:是否可以直接在jQuery脚本上调用.Color-Base来动态设置关联的颜色?

I have a css file. Somewhere in it I have this:

.color-base { color: #f0f0f0; }

My question is: is it possible to call .color-base directly on a jQuery script to dynamically set the associated color?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愚人国度 2025-02-03 05:10:33

是的,您可以通过jQuery动态设置样式。

$(".color-base").css("color", "blue")

Yes, you can dynamically set styles via jQuery.

$(".color-base").css("color", "blue")
忘你却要生生世世 2025-02-03 05:10:33

如果您询问是否可以更新CSS文件,则答案是否。但是,您已经在CSS文件中声明了一个类,并且需要分配给HTML中的某些元素,然后可以使用jQuery更改该元素的颜色,而CSS文件保持不变。像这样,CSS颜色变化部分就像@kenny正确指出的那样:

<div id="testdiv">This text changes color</div>
<input type="button" value="Change color" id="mybtn" onclick="changecolor()"/>
<script>
  function changecolor(){
    $("#testdiv").css("color","#ff0000");
  }
</script>

If you are asking if you can update the CSS file, the answer is NO. However, you have declared a class in the css file and you need to assign to some element in your HTML and then you can change the color of that element using jQuery, while your CSS file remains unchanged. Like this, css color change part is like rightly pointed out by @Kenny:

<div id="testdiv">This text changes color</div>
<input type="button" value="Change color" id="mybtn" onclick="changecolor()"/>
<script>
  function changecolor(){
    $("#testdiv").css("color","#ff0000");
  }
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文