简体   繁体   English

改变一切 <li> 项目背景颜色

[英]change all <li> items background color

I want to change the background of all list items. 我想更改所有列表项的背景。 I have this but it doesn't work. 我有这个,但它不起作用。

// Get all <li> elements in the document
var x = document.querySelectorAll("li"); 

for (let i = 0; < x.length; i++;) {

x[i].style.backgroundColor = "red"; 
}

Typo Error: 错字错误:

Replace for (let i = 0; < x.length; i++;) with for (let i = 0; i < x.length; i++) 替换for (let i = 0; < x.length; i++;) with for (let i = 0; i < x.length; i++)

Yet another typo: ; 还有一个错字: ; after i++ i++

Your JS is correct, it's just having a typo. 你的JS是正确的,它只是有一个错字。

 var x = document.querySelectorAll("li"); for (let i = 0; i < x.length; i++) { x[i].style.backgroundColor = "red"; } 
 li { color: white; text-align: center; border: 1px solid white; font-weight: 900; } 
 <ul> <li>YOUR</li> <li>JAVASCRIPT</li> <li>IS</li> <li>WORKING</li> </ul> 

Use this: 用这个:

var lists = document.getElementsByTagName("li");

// Var or Let works in the for loop

for(let list in lists) {
    lists[list].style.backgroundColor = "black";
}

Not jQuery but basic JavaScript 不是jQuery而是基本的JavaScript

You made a typo error, here the correct one: 你犯了一个拼写错误,这里是正确的错误:

var x = document.querySelectorAll("li"); 

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

Another solution: 另一种方案:

x.forEach((li) => li.style.backgroundColor = "red");

using Jquery, the following line will do the trick. 使用Jquery,以下行将完成这一操作。 $("li").css("background-color","red"); $( “里”)的CSS( “背景色”, “红色”);

Hope this helps 希望这可以帮助

Another approach is to use a little css and Javascript . 另一种方法是使用一点cssJavascript

CSS: CSS:

.red{
    background-color:red
}

jQuery: jQuery的:

$('li').addClass('red')

The jquery then adds a class of red to all li elements. 然后jquery为所有li元素添加一类red In the included example I changed the selector so that it only changes the styling of the desired list. 在包含的示例中,我更改了选择器,使其仅更改所需列表的样式。 In case there are multiple lists on the page. 如果页面上有多个列表。

 $('ol.selected li').addClass('red') 
 .red{ background-color:red } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ol> <li>List item one</li> <li>List item two</li> <li>List item three</li> </ol> <ol class='selected'> <li>List item one</li> <li>List item two</li> <li>List item three</li> </ol> 

Use 采用

Array.
  from(document.getElementsByTagName("li"))
  .map(e => e.style.backgroundColor = "black");

or use 或使用

[]
  .slice
  .call(document.getElementsByTagName("li"))
  .map(e => e.style.backgroundColor = "black");

You might consider to use forEach , instead of map according to your taste. 你可以考虑使用forEach ,而不是根据你的口味来map

PS There are plenty of resources explaining execution time and other acpects between two of them. PS有很多资源可以解释两者之间的执行时间和其他方面的问题。

You can refactor the code using a forEach loop to make it way easier. 您可以使用forEach循环重构代码,以使其更容易。

let x = document.querySelector("li");
x.forEach(function(listElement){
this.style.background = "red";
]);

Thats the easiest non-jquery way of doing this. 这是最简单的非jquery方式。 Picture it like this, a for each loop, is basically saying this: "Ok, so for each list element, I want the background to be red". 想象一下,对于每个循环,基本上是这样说:“好的,所以对于每个列表元素,我希望背景为红色”。

Hope that helped, - Sami 希望有所帮助, - 萨米

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

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