简体   繁体   English

如果 class 包含值,则更改背景颜色

[英]Change background-color if class contains a value

I'm wondering if I can change the background color of a class when it has a specific value.我想知道是否可以在 class 具有特定值时更改它的背景颜色。

Example: if the element contains '1' change bg-color to red, if it contains '2' change bg-color to green, etc.示例:如果元素包含 '1' 将 bg-color 更改为红色,如果它包含 '2' 将 bg-color 更改为绿色,等等。

HTML: HTML:

<div class="skill-points"> 1 </div> <!-- BG color = red -->
<div class="skill-points"> 2 </div> <!-- BG color = green -->
<div class="skill-points"> 3 </div> <!-- BG color = yellow -->

CSS: CSS:

.skill-points {
width: 50px;
padding: 4px;
margin: 4px;
border: 1px solid #000;
text-align: center;
}

How should I write my JavaScript code to accomplish this effect?我应该如何编写我的 JavaScript 代码来实现这个效果?

Just loop through your elements, check the innerText , and set the element style.只需遍历您的元素,检查innerText并设置元素样式。

document.querySelectorAll('.skill-points').forEach(div => {
  if (div.innerText.contains('1')) div.style.backgroundColor = 'red';
  ...
});

You can get all your relevant class elements in a collection (using getElementsByClassName ) and then convert it to an array (using spread operator).您可以在集合中获取所有相关的class元素(使用getElementsByClassName ),然后将其转换为array (使用扩展运算符)。 You can then assign background colours using style.backgroundColor property.然后,您可以使用style.backgroundColor属性分配背景颜色。

I have used a mapping object, that can be useful otherwise you can do individual if checks too.我使用了映射 object,这可能很有用,否则您也可以进行单独的if检查。 innerText is the property you can use to compare the inner Text of the HTML elements. innerText是可用于比较 HTML 元素的内部文本的属性。

 let mapping = { '1': 'red', '2': 'green', '3': 'blue' }; [...document.getElementsByClassName('skill-points')].forEach( x => { x.style.backgroundColor = mapping[x.innerText]; });
 .skill-points { width: 50px; padding: 4px; margin: 4px; border: 1px solid #000; text-align: center; }
 <div class="skill-points"> 1 </div> <!-- BG color = red --> <div class="skill-points"> 2 </div> <!-- BG color = green --> <div class="skill-points"> 3 </div>

1) You can get text using textContent and match it and set the color as 1)您可以使用textContent获取文本并匹配它并将颜色设置为

 const allSkillPoints = document.querySelectorAll(".skill-points"); allSkillPoints.forEach(el => { const text = +el.textContent.trim(); if (text === 1) el.style.backgroundColor = "red"; else if (text === 2) el.style.backgroundColor = "green"; else if (text === 3) el.style.backgroundColor = "yellow"; })
 .skill-points { width: 50px; padding: 4px; margin: 4px; border: 1px solid #000; text-align: center; }
 <div class="skill-points"> 1 </div> <!-- BG color = red --> <div class="skill-points"> 2 </div> <!-- BG color = green --> <div class="skill-points"> 3 </div> <!-- BG color = yellow -->

2) You can also make use of Map here as: 2)您也可以在这里使用Map作为:

 const allSkillPoints = document.querySelectorAll(".skill-points"); const map = new Map() map.set(1, "red").set(2, "green").set(3, "yellow"); allSkillPoints.forEach(el => { const text = +el.textContent.trim(); el.style.backgroundColor = map.has(text)? map.get(text): "white"; })
 .skill-points { width: 50px; padding: 4px; margin: 4px; border: 1px solid #000; text-align: center; }
 <div class="skill-points"> 1 </div> <!-- BG color = red --> <div class="skill-points"> 2 </div> <!-- BG color = green --> <div class="skill-points"> 3 </div> <!-- BG color = yellow -->

... what pretty much everyone else said. ......几乎其他人都说过。 Here's my approach:这是我的方法:

 const valueClassMap = { 1: "bg-red", 2: "bg-green", 3: "bg-yellow" }; document.querySelectorAll(".skill-points").forEach(el => { const content = el.textContent.trim(); if(valueClassMap.hasOwnProperty(content)){ el.classList.add(valueClassMap[content]); } });
 .bg-red{ background: red }.bg-green{ background: green }.bg-yellow{ background:yellow }
 <div class="skill-points"> 1 </div> <!-- BG color = red --> <div class="skill-points"> 2 </div> <!-- BG color = green --> <div class="skill-points"> 3 </div> <!-- BG color = yellow -->

First you need to check if class have value.首先,您需要检查 class 是否有价值。 you have to know that getElementsByClassName() returns an array so you have to access first element (if there is any).您必须知道 getElementsByClassName() 返回一个数组,因此您必须访问第一个元素(如果有的话)。 Then try accessing value property through this javascript code which is detecting the value based on the length of the value.然后尝试通过此 javascript 代码访问值属性,该代码根据值的长度检测值。

  var id = document.getElementsByClassName("classname");
  if (id.length > 0) {
  var x = id[0].value;
  alert(id[0].value);
  }

Then the part where you gonna change the background of the detected class.然后是更改检测到的 class 背景的部分。 You can make if conditions as您可以使 if 条件为

   if x = x {
   document.getElementsByClassName('classname').style.backgroundColor = "red";
   }

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

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