简体   繁体   English

如何从多个元素中添加和删除类?

[英]How to add and remove class from multiple elements?

After many-many hours trying to sort it up I gave up and came here for a help.经过许多小时的尝试,我放弃了,来到这里寻求帮助。

The idea is:这个想法是:

if div has a class active then images within that div should get class active.如果 div 有一个活动类,那么该 div 中的图像应该使类处于活动状态。 If not then images in other divs without class active should have classList.remove('active').如果没有,则其他没有活动类的 div 中的图像应该有 classList.remove('active')。

So the image slider should work only with images that has class pic.所以图像滑块应该只适用于具有类 pic 的图像。

I've tried many ways to solve this.我已经尝试了很多方法来解决这个问题。 But nothing is working.但没有任何效果。 So far I'm trying to solve it this way:到目前为止,我正在尝试以这种方式解决它:

EDITED已编辑

I will try to explain it again.我会尝试再次解释它。 Turned out its not that simple.结果发现没那么简单。

It is a photo gallery slider.这是一个照片库滑块。 The idea is: When you press on <li>cities</li> for example, then you should see photos only related to this li.这个想法是:例如,当您按下<li>cities</li> ,您应该只看到与此 li 相关的照片。 But the structure of the image slider works this way: It shows only one photo on the screen.但是图像滑块的结构是这样工作的:它在屏幕上只显示一张照片。 When you press button prev or next it would change the image.当您按下按钮 prev 或 next 时,它会更改图像。 But should change images only for the chosen tab ( cities, people, landscape, still life).但应该只为所选选项卡(城市、人物、风景、静物)更改图像。

So far image slider changes photos only if an image has a class pic.到目前为止,仅当图像具有类图片时,图像滑块才会更改照片。

In this case I decided to do it this way:在这种情况下,我决定这样做:

If have a class active (class is added when you press on the li and delete when you choose another one) then only images within that div will get class .pic如果有一个类处于活动状态(当您按下 li 时添加类并在您选择另一个时删除类)那么只有该 div 中的图像才会获得类 .pic

Hope it made more clear.希望它说得更清楚。 And thanks for you time谢谢你的时间

**EDITED**

   <div class="info"> 
        <div class="galery_menu">
        <ul>
            <li class="galery" data="cities">cities</li>
            <li class="galery" data="people">people</li>
            <li class="galery" data="landscape">landscape</li>
            <li class="galery" data="still_life">still life</li>
        </ul>
        </div>

    <div class="galery_slider">

        <a class="prev fade"  alt="prev" >❮</a>
        <a class="next fade"  alt="next" >❯</a>
<div class="imgSlider fade active" id="active" data="cities">

    <img src="img/cities/cities_2.jpg" id="img">
    <img src="img/cities/cities_1.jpg" id="img">
    <img src="img/cities/cities_3.jpg" id="img">
    <img src="img/cities/cities_4.jpg" id="img">

</div>
<div class="imgSlider fade" id="active" data="people">
    <img src="img/people/people_1.jpg" id="img" >
    <img src="img/people/people_2.jpg" id="img" >
    <img src="img/people/people_3.jpg" id="img" >
    <img src="img/people/people_4.jpg" id="img" >
    <img src="img/people/people_5.jpg" id="img" >
    <img src="img/people/people_6.jpg" id="img" >
    <img src="img/people/people_7.jpg" id="img" >
    <img src="img/people/people_8.jpg" id="img" >
</div>
</div>
<script>
let picture = document.getElementById('img'),
    active = document.getElementById('active');
if (active.classList.contains('active')) {
    for (let i = 0; i < picture.length; i++){
        picture[i].classList.add('pic');
     }
} else {
    for (let i = 0; i < picture.length; i++){
        picture[i].classList.remove('pic'); 
}}
</script>

Get rid of the duplicate IDs active and img .摆脱重复的 ID activeimg Use classes if needed.如果需要,请使用类。

Pure CSS solution:纯 CSS 解决方案:

 .imgSlider.active img { box-shadow: 0 0 0 4px red; }
 <div class="imgSlider fade active" data="cities"> <img src="https://placehold.it/50x50/0bf"> <img src="https://placehold.it/50x50/f0b"> <img src="https://placehold.it/50x50/bf0"> </div> <div class="imgSlider fade" data="people"> <img src="https://placehold.it/50x50/fb0"> <img src="https://placehold.it/50x50/b0f"> <img src="https://placehold.it/50x50/0fb"> </div>

And if you really need a JS solution...如果你真的需要一个 JS 解决方案......

JavaScript solution: JavaScript 解决方案:

 function handleActivePic () { const EL_parents = document.querySelectorAll(".imgSlider"); EL_parents.forEach(parent => { const isParentActive = parent.classList.contains('active'); const images = parent.querySelectorAll("img"); images.forEach(img => { img.classList[isParentActive?'add':'remove']('pic'); }); }); } handleActivePic(); // Run it!
 .pic { box-shadow: 0 0 0 4px red; }
 <div class="imgSlider fade active" data="cities"> <img src="https://placehold.it/50x50/0bf"> <img src="https://placehold.it/50x50/f0b"> <img src="https://placehold.it/50x50/bf0"> </div> <div class="imgSlider fade" data="people"> <img src="https://placehold.it/50x50/fb0"> <img src="https://placehold.it/50x50/b0f"> <img src="https://placehold.it/50x50/0fb"> </div>

I can't understand exactly what are you trying to do, but if what you need is change the css of the imgs according a class in the parent... check this maybe helps我不明白你到底想做什么,但如果你需要的是根据父类中的类更改 imgs 的 css ......检查这可能有帮助

 .imgSlider.active > img { background-color: red; }
 <!DOCTYPE html> <html> <head> <style> </style> </head> <body> <h1>Welcome to My Homepage</h1> <div class="imgSlider fade active" data="cities"> <img src="img/cities/cities_2.jpg"> <img src="img/cities/cities_1.jpg"> <img src="img/cities/cities_3.jpg"> <img src="img/cities/cities_4.jpg"> </div> <div class="imgSlider fade" data="people"> <img src="img/people/people_1.jpg" > <img src="img/people/people_2.jpg" > <img src="img/people/people_3.jpg" > <img src="img/people/people_4.jpg" > <img src="img/people/people_5.jpg" > <img src="img/people/people_6.jpg" > <img src="img/people/people_7.jpg" > <img src="img/people/people_8.jpg" > </div> </body> </html>

 var divs = document.getElementsByTagName('div'); var cnt = divs.length; for(let i = 0;i<cnt;i++){ var imgs = divs[i].querySelectorAll('img'); var iCnt = imgs.length; if(divs[i].classList.contains('active')){ for(let j=0;j<iCnt;j++){ imgs[j].classList.add('active'); } }else{ for(let j=0;j<iCnt;j++){ imgs[j].classList.remove('active'); } } }
 <div class="imgSlider fade active" id="active" data="cities"> <img src="img/cities/cities_2.jpg" class="img"> <img src="img/cities/cities_1.jpg" class="img"> <img src="img/cities/cities_3.jpg" class="img"> <img src="img/cities/cities_4.jpg" class="img"> </div> <div class="imgSlider fade" id="active" data="people"> <img src="img/people/people_1.jpg" class="img" > <img src="img/people/people_2.jpg" class="img" > <img src="img/people/people_3.jpg" class="img" > <img src="img/people/people_4.jpg" class="img active" > <img src="img/people/people_5.jpg" class="img" > <img src="img/people/people_6.jpg" class="img" > <img src="img/people/people_7.jpg" class="img" > <img src="img/people/people_8.jpg" class="img" > </div>

You can use Each in your elements and use classList.add/remove/toggle您可以使用Each在你的元素,并且使用classList.add/remove/toggle

document.querySelectorAll(".elem").forEach(e => {}
    e.classList.add("any");
);

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

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