如何选择具有以以下内容开头的属性的所有元素 [英] How to select all elements with an attribute that starts with
本文介绍了如何选择具有以以下内容开头的属性的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图选择所有以框"开头的元素.这是我现有的代码:
I am trying to select all the elements that start with the "box". This is my existing code:
if (($(this).attr('title') == 'Box a') || ($(this).attr('title') == 'Box b')) || ($(this).attr('title') == 'Box c')) {
// do stuff
}
有什么方法可以缩短这个时间?
Is there a way to shorten this?
谢谢
推荐答案
使用CSS符号是可能的!
Using CSS symbols it is possible!
^
符号.
$(element[title^='box']);
选择标题字段以box
开头的所有element
元素.
That selects all element
elements that the title field starts with box
.
更新:
尝试一下.
var regEx = /^Box/;
if(regEx.test($(this).attr('title'))){
}
这篇关于如何选择具有以以下内容开头的属性的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文