简体   繁体   English

如何使用javascript选择元素的所有子元素并更改CSS属性?

[英]How to select all children of an element with javascript and change CSS property?

I am working on a project that has a div with 32 children.我正在做一个有 32 个孩子的 div 的项目。 I need to create a dropdown menu that will change the background of each div and the parent.我需要创建一个下拉菜单来更改每个 div 和父级的背景。 For the other parts of the project that don't have children, I have been using the following code:对于项目中没有孩子的其他部分,我一直在使用以下代码:

function changediv(color) {
document.getElementById('div1').style.background = color;
}

HTML: HTML:

<select>
<option onClick="changediv('#555');">Hex</option>
<option onClick="changediv('blue');">Colorname</option>
<option onClick="changediv('url(example.com/example.png)');">Image</option>
</select>

I could just add a different ID to each child (id1,id2,id3,...), but there are 32 children and not only would I have to add 32 IDs, but also 32 lines of Javascript.我可以给每个孩子添加一个不同的 ID (id1,id2,id3,...),但是有 32 个孩子,我不仅要添加 32 个 ID,还要添加 32 行 Javascript。 There has to be a better way;一定有更好的方法; somehow selecting children or even changing the actual CSS code that selects the children.以某种方式选择子项,甚至更改选择子项的实际 CSS 代码。

Thanks, Ian谢谢,伊恩

While this can be done in one line with JQuery, I am assuming you are not using JQuery - in which case, your code will be:虽然这可以在一行中使用 JQuery 完成,但我假设您没有使用 JQuery - 在这种情况下,您的代码将是:

var nodes = document.getElementById('ID_of_parent').childNodes;
for(var i=0; i<nodes.length; i++) {
    if (nodes[i].nodeName.toLowerCase() == 'div') {
         nodes[i].style.background = color;
     }
}

See http://jsfiddle.net/SxPxN/ for a quick example I created - Click on "change 'em" to see it in action有关我创建的快速示例,请参见http://jsfiddle.net/SxPxN/ - 单击“更改 'em”以查看它的实际效果

Try to use below codes:尝试使用以下代码:

var nodes = document.getElementById('ID_of_parent').getElementsByTagName("div");
for(var i=0; i<nodes.length; i++) {
    nodes[i].style.background = color;
}

If there's only one parent element then we can use querySelector to select all the children of that element如果只有一个父元素,那么我们可以使用querySelector来选择该元素的所有子元素

HTML HTML

<div class="parent">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>

JS JS

let children = document.querySelector(".parent").children;
children.style.color = "red";

If there are more parent elements having same class then we can use querySelectorAll and forEach如果有更多具有相同类的父元素,那么我们可以使用querySelectorAllforEach

HTML HTML

<div class="parent">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>
<div class="parent">
  <div>7</div>
  <div>8</div>
  <div>9</div>
</div>

JS JS

let children = document.querySelectorAll(".parent").children;
children.forEach(child => {
  child.style.color = "red";
})
var children = document.getElementById("div").children;
var i;
for (i = 0; i < children.length; i++) {
  children[i].style.visibility = "hidden";
}

I am creating a variable called children and in it I am getting the element with the id "div" and then using .children to select the children and put it into an array.我正在创建一个名为 children 的变量,并在其中获取 ID 为“div”的元素,然后使用 .children 选择子项并将其放入数组中。 Then I create var i and use a for loop to go through and change all of their CSS value.然后我创建 var i 并使用 for 循环遍历并更改它们的所有 CSS 值。 The program stops after it has gone through each of the child elements.程序在遍历每个子元素后停止。

Simple you can use for each loop简单,您可以用于每个循环

HTML HTML

<div id="wrapper">
    <div class="el">Element 1</div>
    <div class="el">Element 2</div>
    <div class="el">Element 3</div>
</div

JavaScript JavaScript

const element =document.getElementById('wrapper');
if(element.hasChildNodes()){
element.childNodes.forEach((e, i)=>{
    console.log(e);
});
}

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

相关问题 如何在 Javascript 中获取元素及其子元素的所有计算 css 属性 - How get all computed css properties of element and its children in Javascript 如何递归选择元素Javascript下的所有子项 - How to recursively select all children under an element Javascript 使用javascript选择DOM的所有元素,但特定元素的所有子元素除外 - Select all element of the DOM with javascript except all the children of a particular element 如何通过 css 值获得元素的 select 子项 - how to select children of element by css value 将鼠标悬停在子元素上时如何 select 所有子元素 - How to select all children elements when hovered over a children element 1状态属性的更改触发整个元素和所有子元素的重新渲染 - Change in 1 state property triggers re render of whole element and all children 如何根据上的函数更改javascript css图像 <select>元件? - How to change a javascript css image based on a function on the <select> element? 如何使用JavaScript更改CSS属性? - How to change a css property with javascript? 如何使用AngularJS更改DOM元素的CSS属性 - How to change the CSS property of the DOM element with AngularJS TypeScript或JS-如何选择元素及其所有子元素进入HTMLCollection? - TypeScript or JS- How to select an element and all its children into a HTMLCollection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM