简体   繁体   English

如果一个类包含某个名称,请更改该元素的样式

[英]If a class contains a certain name, change the style of that element

What I currently have in my JS file: 我当前在JS文件中拥有什么:

if (documentElement.className("child") != -1){
    documentElement.className("child").backgroundColor="red";
}

Basically as the title implies, I want all the classes with the name "child" on the page to change their background color to red. 基本上如标题所示,我希望页面上所有名称为“ child”的类将其背景颜色更改为红色。

className returns a string with all of the classes on the element. className返回一个包含元素上所有类的字符串。 You need to see if that string contains your class: 您需要查看该字符串是否包含您的类:

if (documentElement.className.indexOf("child") != -1){

Use indexOf 使用indexOf

Regarding what @Roberrrt said - you can achieve this simply selecting the 'child class' and applying a style to it. 关于@Roberrrt所说的-您只需选择“子类”并为其应用样式即可实现此目的。

For a visual example. 作为一个直观的例子。

http://codepen.io/anon/pen/GNMJjr http://codepen.io/anon/pen/GNMJjr

In your code: 在您的代码中:

documentElement.className("child").backgroundColor="red";

That wouldn't work, this is a better way of achieving what you want: 那是行不通的,这是实现您想要的更好的方法:

document.querySelector('.child').style.backgroundColor = "red";

I still recommend using CSS. 我仍然建议使用CSS。 This is redundant and extremely verbose. 这是多余的,而且非常冗长。

Try this maybe (tested on about:blank page with 5 <div> with 2 of them with the class="red" : 也许尝试一下(在about:blank页面上用5 <div>进行测试,其中2个使用class="red"

var red = [];
red = document.querySelectorAll(".red");

for (var i = 0; i < red.length; i += 1) {
     red[i].style.backgroundColor = "red";
}

If you really want to verify if the class name contains a string, use classList.contains (it often have resolved this kind of problem for me, check the MDN page ) 如果您确实要验证类名是否包含字符串,请使用classList.contains (它通常已经为我解决了此类问题,请查看MDN页面

暂无
暂无

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

相关问题 更改包含指定单词的元素的样式 - Change style of an element which contains a specified word 包含某些类字符串的jQuery显示元素 - jQuery display element that contains certain class strings 如果子元素包含某些字符串链,如何更改父元素的 CSS class - How to change the CSS class of a parent element if the child contains certain string chain 检查元素是否具有某个 class 并且属于包含某个值的表单 - Check if element has a certain class and belongs to a form that contains a certain value 如果span元素包含具有特定类的div,则隐藏一个元素 - hide an element if span element contains div with a certain class 如何检查单击的元素是否包含具有特定类的元素 - How to check if clicked element contains element with certain class 如何在不使用JS的ID的情况下按类名更改元素的样式属性? - How to change a style property of element by class name without any use of ID's in JS? vue.js —无法通过css更改svg的样式,在dom中它应用element.style而不是类名 - vue.js — can't change style of svg through css, and in dom it applies element.style instead of class name 如何检查元素是否具有特定样式以及是否具有样式更改其他元素的样式? - How to check if the element has certain style and if it has style change the style of another element? JS 更改具有特定 id 和 class 名称的元素 - JS alter element with certain id and class name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM