简体   繁体   中英

How to edit css style using javascript with complete css code for any component

I'm working on a CSS editor section for my page, I have to add an advanced CSS editor. This editor should apply changes to the site's css using CSS code through a textarea, for example

#para1 {
  text-align: center;
  color: red;
}

how can I edit this css style for "para1"?

NOTE: PARA1 is only an example, actually what I want is that it can identify any ID that I put in it

Try this if you use a class :

$(".para1, .para2").css({"text-align": "left", "color": "blue"});

Try this if you use an id :

$("#para1, #para2").css({"text-align": "left", "color": "blue"});

If you're using vanilla JavaScript and I understood it correctly, this is what you need:

 var para1 = document.getElementById("para1"); para1.style.color = "red"; para1.style.textAlign = "center";
 <p id="para1">Paragraph</p>

using javascript,

let comp = document.querySelector("#para1");
comp.classList.add("newStyles");

This will add newStyles class to comp variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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