如何使用JS为除特定图像之外的所有图像添加类 [英] How to add a class to all images except specific ones with JS

查看:63
本文介绍了如何使用JS为除特定图像之外的所有图像添加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了两个特定的图像外,我想为所有图像添加一个类.因为我不想将该类添加到徽标中.

I want to add a Class to all images except for two specific ones. Because i don't want to add that Class to the logo for example.

这就是我现在拥有的:

$('img').addClass('class_two');

我不想在其上添加此类的图像是:

The images where i don't want to add this class on are:

<img id="id_1" src="danielgotzlogo.png" alt="danielgotzlogo">
<img id="id_2" src="xxsbanner.jpg" alt="#">

但这会将其添加到所有图像.我如何不将班级添加到特定班级?

But this adds it to all images. How can i not add the Class to specific ones?

推荐答案

给出您不想影响的两个元素的特定ID:

Given the specific IDs of the two elements you don't want to affect:

$('img').not('#id1').not('#id2').addClass('myclass');

或:

$('img').not('#id1,#id2').addClass('myclass');

或:

$('img:not(#id1,#id2)').addClass('myclass');

这篇关于如何使用JS为除特定图像之外的所有图像添加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆