使用jquery在body [all elements]中搜索一个类
Search for a class in body [all elements] using jquery
我需要搜索有像show-item-10
类的html页面,我必须分割它例如,我有以下div
标签在我的html页面,我想找到元素使用类名sell-item-*
<div class="sell-item-10 item_container">Item 1</div>
<div class="buy-item-2 item_container">Item 2</div>
<div class="sell-item-22 item_container">Item 3</div>
<div class="buy-item-3 item_container">Item 4</div>
<div class="sell-item-20 item_container">Item 5</div>
<div class="sell-item-20 item_container">Item 6</div>
我只想得到我搜索的二级类item_container
您可以直接使用选择器:.item_container[class*="sell-item"]
。
Where [attr*=value]
表示一个属性名为attr的元素,其值至少包含一个字符串"value"作为子字符串出现。(mdn)
例子
$('.item_container[class*="sell-item"]');
一个(相对)简单的方法:
// selecting each element with a class attribute, then iterating over that
// collection, using each:
$('[class]').each(function(){
// caching the classnames in an array (splitting the string using split()):
var classes = this.className.split(/'s/);
// iterating over each member of that array:
for (var i = 0, len = classes.length; i < len; i++){
// if the current class-name starts with 'sell-item':
if (classes[i].indexOf('sell-item') === 0) {
// we set properties of the current element-node over which we're
// iterating (with 'each()').
// classes[i] is the full class-name (that started with 'sell-item',
// classes[i].replace(...) is replacing not-numbers ('D), with empty
// strings (to reduce that string to just the number components):
this.sellItemClassName = classes[i];
this.sellItemNumber = classes[i].replace(/'D/g,'');
}
}
}).text(function(i,t){
// almost irrelevant, just to show how to retrieve the properties we set:
return t + ' (' + this.sellItemClassName + ', ' + this.sellItemNumber + ')';
});
JS Fiddle demo.
引用:
- JavaScript:
- JavaScript正则表达式
-
String.replace()
. -
String.split()
.
-
each()
. -
text()
.
相关文章:
- 检查搜索结果是否存在多次如果是,则在Javascript中只显示一个结果
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 正则表达式搜索一个空格,后跟一个字符
- 如何为一个搜索框实现两个高级自动补全
- 我需要迭代一个JSON数组——不知道如何做到——已经搜索过了,但仍然可以'我一点也不知道
- 如何使用 JavaScript 正则表达式搜索除一个实例之外的所有内容
- Javascript-在一个数组中搜索整个单词,并创建一个符合条件的新数组
- 一页主题搜索/用下一个按钮突出显示文本
- Regex,它搜索一个起始字符,介于两者之间的任何字符,然后是一个结束字符
- 在Meteor(MongoDB)中,按另一个集合中的值搜索一个集合
- 搜索一个多维数组
- 正则表达式在csv中搜索一个精确的字符串
- 在Javascript页面中搜索-一个结果错误
- 搜索一个文档并更新子文档的一些字段
- 是否有可能搜索一个布尔在蒙古与ajax帖子没有服务器端转换
- 搜索一个嵌套的javascript对象,获得一个祖先数组
- 在多个数组中搜索一个字符串,然后设置一个jQuery变量
- 搜索一个对象并获得等于某个键的项
- 搜索一个不可变的JS Map
- 使用jquery在body [all elements]中搜索一个类