在css表和html元素id中使用javascript

时间:2022-11-19 09:58:58

I'm developing in Javascript, css, html..

我正在开发Javascript,css,html ..

In this moment I have a lot of css classes referenced in my Javascript code and now I'm creating "constants" with the names of the css classes. This is because if I want to change the name of one of this classes, I should just do it in one place.

在这一刻,我的Javascript代码中引用了很多css类,现在我正在创建带有css类名称的“常量”。这是因为如果我想更改其中一个类的名称,我应该在一个地方进行。

My questions are two.

我的问题是两个。

The first is

首先是

  • Can I use Javascript variables to define classes and ids into the css file and ids of elements inside html documents. Some like this
  • 我可以使用Javascript变量将类和ID定义到css文件和html文档中的元素ID。有些像这样

For CSS (replacing "&&js_var&&" by a sentence that sets the id to the value of javascript variable)

对于CSS(用一个将id设置为javascript变量值的句子替换“&& js_var &&”)

. &&js_var&& {
   /* css definition */
}

for HTML (replacing "&&js_var&&" by a sentence that sets the id to the value of javascript variable)

for HTML(用一个将id设置为javascript变量值的句子替换“&& js_var &&”)

<div id="&&js_var&&"></div>

The second is,

第二个是,

  • in case that it can be done, is this good programming practice?
  • 如果可以做到,这是一个很好的编程实践吗?

1 个解决方案

#1


1  

First question - Can I use Javascript variables to define classes and ids into the css file and ids of elements inside html documents.

第一个问题 - 我可以使用Javascript变量将类和ID定义到css文件和html文档中的元素ID。

Answer: No, not in the way you are thinking. Css content are not subject to dynamic manipulation, but are plain text rendered by the browser. You could, in a certain way, replace some of the text content and re-render the style using some techniques, but it falls into your second question.

答:不,不是你想的那样。 Css内容不受动态操作的约束,而是由浏览器呈现的纯文本。您可以以某种方式替换一些文本内容并使用某些技术重新呈现样式,但它属于您的第二个问题。


Second question - in case that it can be done, is this good programming practice?

第二个问题 - 如果可以做到,这是一个很好的编程实践吗?

Answer: Not at all. Css stylesheets are designed to center the visual styles applied to html elements in one place, causing every visual change to have a cascading effect on every page refering to this stylesheet (there is where the name .css come from, "cascading"). The whole point is that class names and element's ids are static, so the cascading would make sense.

答:完全没有。 Css样式表旨在将应用于html元素的视觉样式集中在一个位置,导致每个视觉变化对引用此样式表的每个页面都有级联效果(名称.css来自“cascading”)。整点是类名和元素的id是静态的,所以级联是有意义的。


I hope it helped some way...

我希望它有所帮助......

#1


1  

First question - Can I use Javascript variables to define classes and ids into the css file and ids of elements inside html documents.

第一个问题 - 我可以使用Javascript变量将类和ID定义到css文件和html文档中的元素ID。

Answer: No, not in the way you are thinking. Css content are not subject to dynamic manipulation, but are plain text rendered by the browser. You could, in a certain way, replace some of the text content and re-render the style using some techniques, but it falls into your second question.

答:不,不是你想的那样。 Css内容不受动态操作的约束,而是由浏览器呈现的纯文本。您可以以某种方式替换一些文本内容并使用某些技术重新呈现样式,但它属于您的第二个问题。


Second question - in case that it can be done, is this good programming practice?

第二个问题 - 如果可以做到,这是一个很好的编程实践吗?

Answer: Not at all. Css stylesheets are designed to center the visual styles applied to html elements in one place, causing every visual change to have a cascading effect on every page refering to this stylesheet (there is where the name .css come from, "cascading"). The whole point is that class names and element's ids are static, so the cascading would make sense.

答:完全没有。 Css样式表旨在将应用于html元素的视觉样式集中在一个位置,导致每个视觉变化对引用此样式表的每个页面都有级联效果(名称.css来自“cascading”)。整点是类名和元素的id是静态的,所以级联是有意义的。


I hope it helped some way...

我希望它有所帮助......