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.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.user').removeClass('myclass');
This would be both faster and cleaner.
这将更快更清洁。