[英]Add class to parent div for each instance of class
I have a wrapper div called container and inside that container div I have multiple instances of a div called mix. 我有一个称为容器的包装div,在该容器div中,我有一个名为mix的div的多个实例。 Inside each mix
there is a class called changeclass
with a text value. 在每个mix
都有一个名为changeclass
的类,带有文本值。 I need to add the text value of each changeclass
div to its parent mix div as a class. 我需要将每个changeclass
div的文本值添加到其父类div作为一个类。
Here is my HTML. 这是我的HTML。
<div class="mix">
<div class="classchange">
<p>active</p>
<div class="text"></div>
</div>
</div>
<div class="mix">
<div class="classchange">
<p>active2</p>
<div class="text"></div>
</div>
</div>
<div class="mix">
<div class="classchange">
<p>active3</p>
</div>
</div>
<div class="mix">
<div class="classchange">
<p>active4</p>
<div class="text"></div>
</div>
</div>
So far in my research, I have been able to add the class the parent div of one but not each individual instance of this. 到目前为止,在我的研究中,我已经能够将一个类的父div添加到该类中,但不能将其添加到每个单独的实例中。
I've been using this 我一直在用这个
var str = $( ".classchange" ).text();
$( ".classchange" ).parent().addClass( str );
But I'm not sure how to add .each function to it to make it work. 但是我不确定如何向其添加.each函数以使其正常工作。
Iterate over .each
of the .mix
es, selecting the text in each one's classchange
and calling addClass
on the .mix
with that text: .each
.mix
的classchange
,在每个人的classchange
选择文本,并使用该文本在.mix
上调用addClass
:
$('.mix').each(function() { $(this).addClass( $(this).find('p').text() ); }); console.log(document.body.innerHTML);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="mix"> <div class="classchange"> <p>active</p> <div class="text"></div> </div> </div> <div class="mix"> <div class="classchange"> <p>active2</p> <div class="text"></div> </div> </div> <div class="mix"> <div class="classchange"> <p>active3</p> </div> </div> <div class="mix"> <div class="classchange"> <p>active4</p> <div class="text"></div> </div> </div>
Well, since all your classchange
are inside mix
, u could document.querySelectorAll(path)
which will return ua NodeList
. 好吧,由于您所有的classchange
都在mix
里面,所以您可以document.querySelectorAll(path)
返回ua NodeList
。
Then, u could convert it to an array data structure and apply map
or forEach
operations on them, where u could work with each classChange
node and it's parent/s. 然后,您可以将其转换为数组数据结构,并对其应用map
或forEach
操作,在其中您可以使用每个classChange
节点及其父节点。
This is what I would do in ES6, not sure about JQuery but I'll let this here in case it helps at all. 这是我在ES6中要做的事情,不确定JQuery,但是如果有帮助,我将在此保留。
So, you want to add the new class to each .mix
element right? 因此,您想向每个.mix
元素添加新类,对吗?
If so, then we could iterate on each .classchange
element and add the new class from there. 如果是这样,那么我们可以迭代每个.classchange
元素,然后从那里添加新的类。 Like so: 像这样:
$('.classchange').each(function() {
$(this).parent('.mix').addClass($(this).text());
});
To better control the behavior of each matching element use the $.each() function to loop over the target elements and control the DOM manipulation of each parent .mix
individually: 为了更好地控制每个匹配元素的行为,请使用$ .each()函数遍历目标元素并分别控制每个父.mix
的DOM操作:
// Loop through all .classchange div $('.classchange').each(function() { // Select closest .mix div add add new class based on current target text $(this).closest('.mix').addClass($(this).text()); });
.active { color: red; } .active2 { color: hotpink; } .active3 { color: purple; } .active4 { color: blue; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="mix"> <div class="classchange"> <p>active</p> <div class="text"></div> </div> </div> <div class="mix"> <div class="classchange"> <p>active2</p> <div class="text"></div> </div> </div> <div class="mix"> <div class="classchange"> <p>active3</p> </div> </div> <div class="mix"> <div class="classchange"> <p>active4</p> <div class="text"></div> </div> </div>
Your code was basically adding all 4 active classes to all .mix
divs. 您的代码基本上是将所有4个活动类添加到所有.mix
div中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.