从以username开头的所有ID中删除一个类

时间:2022-06-15 15:20:28

I have a bunch of elements that look like

我有一堆看起来像的元素

<div id="username-2343"></div>

I need to loop through all of them on the page and remove a class from them using jQuery.

我需要在页面上遍历所有这些并使用jQuery从它们中删除一个类。

How can I do this? I don't know all the ID's so I need to search for all matching username.

我怎样才能做到这一点?我不知道所有的ID,所以我需要搜索所有匹配的用户名。

1 个解决方案

#1


This should do it with the attributeStartsWith selector:

这应该使用attributeStartsWith选择器:

$('div[id^=username-]').removeClass('myclass');

Presumably, however, you have control of these <div>s as they are printed onto the page. It would be in better taste to simply give them all a class signifying they are a "user div", so then you can just do:

但是,据推测,当您将这些

打印到页面上时,您可以控制它们。简单地给他们所有类别表示他们是“用户div”,这将是更好的品味,所以你可以这样做:

$('div.user').removeClass('myclass');

This would be both faster and cleaner.

这将更快更清洁。

#1


This should do it with the attributeStartsWith selector:

这应该使用attributeStartsWith选择器:

$('div[id^=username-]').removeClass('myclass');

Presumably, however, you have control of these <div>s as they are printed onto the page. It would be in better taste to simply give them all a class signifying they are a "user div", so then you can just do:

但是,据推测,当您将这些

打印到页面上时,您可以控制它们。简单地给他们所有类别表示他们是“用户div”,这将是更好的品味,所以你可以这样做:

$('div.user').removeClass('myclass');

This would be both faster and cleaner.

这将更快更清洁。