Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

html - How can I add two style element inside the javascript?

How can I add two style element inside the javascript?

In my example i want to change the arrow to red for the class shadow. so i need to add css to the javascript code. I already have background-color: red !important. how can i add another css code inside the style?

I want the arrow of the li a.shadow to be red.

var divs = document.querySelectorAll('li a.shadow'),
  i;

for (i = 0; i < divs.length; ++i) {
  divs[i].parentNode.setAttribute('style', 'background-color: red !important');
}
.breadcrumb {
  margin: 20px;
  list-style: none;
  font: 15px Helvetica, Arial, Sans-Serif;
  padding: 0;
  display: flex;
}

.breadcrumb li {
  border-top: 1px solid black;
  border-bottom: 1px solid black;
  border-left: 1px solid white;
  color: black !important;
  text-decoration: none;
  background: brown;
  /* fallback color */
  background: hsla(34, 85%, 35%, 1);
  position: relative;
  display: flex;
  align-items: center;
}

.breadcrumb li:nth-child(2) {
  background: rgb(221 221 221) !important;
  border-left: 1px solid black !important;
}

.breadcrumb li:last-child {
  background: rgb(221 221 221) !important;
  border-right: 1px solid black !important;
}

.breadcrumb a,
.breadcrumb a:link,
.breadcrumb a:visited,
.breadcrumb a:active {
  color: #212121;
  margin: 0 0px;
}

.breadcrumb li span::before {
  content: " ";
  display: block;
  width: 0;
  height: 0;
  border-top: 15px solid transparent;
  border-bottom: 15px solid transparent;
  border-left: 15px solid white;
  position: absolute;
  top: 50%;
  margin-top: -15px;
  margin-left: 1px;
  left: 100%;
  z-index: 1;
}

.breadcrumb li a::before {
  content: " ";
  display: block;
  width: 0;
  height: 0;
  border-top: 15px solid transparent;
  border-bottom: 15px solid transparent;
  border-left: 15px solid white;
  position: absolute;
  top: 50%;
  margin-top: -15px;
  margin-left: 2px;
  left: 100%;
  z-index: 1;
}

.breadcrumb li:before {
  content: "";
  display: block;
  width: 0;
  height: 0;
  border-top: 15px solid transparent;
  /* Go big on the size, and let overflow hide */
  border-bottom: 15px solid transparent;
  border-left: 15px solid transparent;
  position: relative;
  left: 100%;
  z-index: 2;
}

.breadcrumb li:last-child:before {
  border-left: 0px solid hsla(34, 85%, 35%, 1) !important;
  padding-left: 10px;
  padding-right: 10px;
}

.breadcrumb li:nth-child(8) {
  background: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(8):before {
  border-left-color: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(7) {
  background: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(7):before {
  border-left-color: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(6) {
  background: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(6):before {
  border-left-color: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(5) {
  background: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(5):before {
  border-left-color: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(4) {
  background: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(4):before {
  border-left-color: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(3) {
  background: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(3):before {
  border-left-color: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(2) {
  background: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(2):before {
  border-left-color: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(1) {
  background: rgb(221 221 221) !important;
}

.breadcrumb li:nth-child(1):before {
  border-left-color: hsla(34, 85%, 85%, 1) !important;
}

.border {
  width: 100%
}
<ul class="breadcrumb">
  <li> <a href="" class="shadow">Link with shadow</a></li>
  <li> <a href="" class="shadow">Link with shadow</a></li>
  <li> <a href="">Link without shadow</a></li>
  <li> <a href="">Link without shadow</a></li>
  <li> <a href="" class="shadow">Link with shadow</a></li>
</ul>
question from:https://stackoverflow.com/questions/65546048/how-can-i-add-two-style-element-inside-the-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can add as many as you want, just separate them by ;

var divs = document.querySelectorAll('li a.shadow'),
  i;

for (i = 0; i < divs.length; ++i) {
  divs[i].parentNode.setAttribute('style', 'background-color: red !important; color: #fff;');
}

With that being said, if you plan on adding multiple styles it's probably better to add a class that contains all those styles instead, like this:

const element = document.getElementById("myDIV");
element.classList.add("mystyle");
.mystyle {
  width: 100%;
  padding: 25px;
  background-color: coral;
  color: white;
  font-size: 25px;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...