简体   繁体   English

如何直接更改 css class?

[英]how to alter a css class directly?

Is it possible, to use javascript to edit the contents of a.css file directly?是否可以使用 javascript 直接编辑 a.css 文件的内容? (In turn, impacting the style of the elements that use the class being modified?) (反过来,影响使用正在修改的 class 的元素的样式?)

I've seen a few answers to such a question, but they all deal with the dynamic creation of HTML and css code in the html document itself, which I don't quite want...我已经看到了这样一个问题的一些答案,但它们都处理 html 文档本身中 HTML 和 css 代码的动态创建,我不太想要......

Say for example, I have the following code:例如,我有以下代码:

*{
    color:red;
}

This makes ALL the text in the document go red.这会使文档 go 中的所有文本变为红色。 If I give the client an option to drop down and change the color of all the text to whatever he/she wishes, then the most logical choice is for me to go into the.css file, and edit the value of 'color' under the universal selector...如果我让客户选择下拉并将所有文本的颜色更改为他/她想要的任何颜色,那么最合乎逻辑的选择是我将 go 放入 .css 文件中,并在下编辑“颜色”的值通用选择器...

Ideas?想法?

You can do all your css manipulation in a single style element you append to the head- anything you put there takes precedence over the other stylesheets, and your users can 'backtrack' to the page defaults by deleting their changes, one at a time or all at once.您可以在 append 到头部的单个样式元素中执行所有 css 操作 - 您放置在那里的任何内容都优先于其他样式表,并且您的用户可以通过删除他们的更改来“回溯”到页面默认值,一次一个或一次全部。

Using the last sheet (your new style element's rules) in the document.styleSheets collection, you can append new rules, delete old rules, or edit existing rules.使用 document.styleSheets 集合中的最后一张表(新样式元素的规则),您可以 append 新规则、删除旧规则或编辑现有规则。

And you have a piece of text you can save on your server or in local storage for each user, possibly to add to the document the next time they visit.您可以将一段文本保存在您的服务器上或每个用户的本地存储中,可能会在他们下次访问时添加到文档中。

The way I would approach this is to save the preference in something like a profile and when the page loads, dynamically apply the preference using the page generator or using Javascript.我解决这个问题的方法是将首选项保存在配置文件中,当页面加载时,使用页面生成器或使用 Javascript 动态应用首选项。 Using the page generator would make things smoother as the CSS code will be in the page itself when it loads.使用页面生成器会使事情变得更顺畅,因为 CSS 代码在加载时将在页面本身中。

You can't add to the CSS with JavaScript, but you can add styles to an element or a series of elements.您不能将 CSS 与 JavaScript 添加到 CSS,但您可以将 styles 添加到一个元素或一系列元素。

If you want to give your clients control over the styles you can have them created with PHP.如果您想让您的客户控制 styles,您可以使用 PHP 创建它们。 You'd basically just use your variables as values and have them link to the PHP file in the CSS link element.您基本上只需将变量用作值并将它们链接到 CSS 链接元素中的 PHP 文件。

<link rel="stylesheet" type="text/css" href="/path/to/css.php" />

In short, it's possible to use javascript to change css.简而言之,可以使用 javascript 来更改 css。 You can use jQuery and there are some examples here: http://api.jquery.com/css/ .您可以使用 jQuery,这里有一些示例: http://api.jquery.com/css/

You could do...你可以做...

var elements = document.getElementsByTagName('body')[0].getElementsByTagName('*');

for (var i = 0, length = elements.length; i < length; i++) {
    elements[i].style.color = '#f00';
}

I am assuming that you want to alter the original.css file on the server, so that style change is maintained for the following requests.我假设您要更改服务器上的 original.css 文件,以便为以下请求维护样式更改。 But JavaScript executes in the browser and.css files live on the server.但是 JavaScript 在浏览器中执行,.css 文件在服务器上。 So the short answer is, no, it cannot be done.所以简短的回答是,不,它不能完成。 Not practically anyway.反正不是。

You could, if you really wanted to, create some JavaScript that makes an AJAX request to the server and then write some server side code to receive the request and modify the.css file accordingly.如果你真的想,你可以创建一些 JavaScript 向服务器发出 AJAX 请求,然后编写一些服务器端代码来接收请求并相应地修改 .css 文件。 That would more trouble than it would be worth, and probably create an entertaining security vulnerability, so I would not recommend it.这会带来更多麻烦,而且可能会产生有趣的安全漏洞,所以我不推荐它。

Dynamically load CSS file, using pure JavaScript动态加载 CSS 文件,使用纯 JavaScript


var filename = "matrix.css";
var css      = document.createElement("link");

css.setAttribute("rel", "stylesheet");
css.setAttribute("type", "text/css");
css.setAttribute("href", filename);
document.getElementsByTagName("head")[0].appendChild(css);

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

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