简体   繁体   English

添加/修改CSS中的CSS <style> tag on with jQuery/javascript

[英]Add/Modify CSS in a <style> tag on with jQuery/javascript

What I want to do: 我想做的事:

I want to add rules and modify existing rules inside of a <style> tag with javascript/jQuery. 我想用javascript / jQuery添加规则并修改<style>标记内的现有规则。

Why I want to do it: 为什么我要这样做:

I'm basically building a color scheme editor and when the user changes a value in the form, I want to update the CSS to reflect this change. 我基本上是构建一个配色方案编辑器,当用户更改表单中的值时,我想更新CSS以反映这一变化。

What I've Tried: 我试过的:

Replacing the entire <style> tag with CSS generated by a JS function. 用JS函数生成的CSS替换整个<style>标记。 This works but seems like a lot of processing for a very small change. 这可行但似乎很多处理只是一个非常小的变化。

Also I can create container <div> s that contain specific CSS classes and simply find/replace their HTML. 此外,我可以创建包含特定CSS类的容器<div> ,只需查找/替换它们的HTML。 Exmaple below: 例如:

  <div id="button_background_cont">
    <style type="text/css">.buttons {background-color:#333333;}</style>
  </div>

  <div id="button_color_cont">
    <style type="text/css">.buttons {color:#ffffff;}</style>
  </div>

These both work... but are really clunky and I don't particularly like them. 这些都有效...但是真的很笨重,我不是特别喜欢它们。

Example Of What I Need To Do: 我需要做的例子:

<style type="text/css" id="color_scheme">
   .buttons {background:#333333; color:#ffffff;}
</style>
<input name="button_background" id="button_background" value="#333333" />
<input name="button_color" id="button_color" value="#ffffff" />

Change The form elements and make it the following 更改表单元素并使其如下所示

<style type="text/css" id="color_scheme">
   .buttons {background:#ff0000; color:#333333;}
</style>
<input name="button_background" id="button_background" value="#ff0000" />
<input name="button_color" id="button_color" value="#333333" />

Any ideas? 有任何想法吗?

This would be a basic way of doing it, you could perhaps find a better way to generate styles string, but it would look something like this: 这将是一种基本的方法,你可能会找到一种更好的方式来生成样式字符串,但它看起来像这样:

    function generateStylesContent(){
        var btnBgr = $("#button_background").val();
        var btnColor = $("#button_color").val();
        var stylesPlaceholder = ".buttons{background:[BACKGROUND];color:[COLOR];}";

        var newGeneratedStyles = stylesPlaceholder
                                 .replace("[BACKGROUND]", btnBgr)
                                 .replace("[COLOR]", btnColor)    

        $('#color_scheme').text(newGeneratedStyles);
    };

    $('.buttons').on('click', function(){
        generateStylesContent();    
    })

JSFIDDLE 的jsfiddle

Updated 更新

你可以用以下方式使用j-query来做到这一点

$("#button_background").css("color",'blue')

While you can do that there is easier ways to do (check here and here ), for example: using the class selector you can filter all objects that have the "old" class $( ".oldClass" ) and manipulate what classes they have (instead editing the class) : 虽然你可以做到这一点有更简单的方法( 在这里这里检查),例如:使用类选择器,你可以过滤所有具有“旧”类$( ".oldClass" )并操纵他们有什么类(而不是编辑类):

filter and add the new class 过滤并添加新类

$( ".oldClass" ).addClass(".newClass")

and remove the old class 删除旧类

$( ".oldClass" ).removeClass(".oldClass")

you can also use .css to change a property : 你也可以使用.css来改变一个属性:

$( ".oldClass" ).css("background","#ff0000")

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

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