如何选择具有以以下内容开头的属性的所有元素

如何选择具有以以下内容开头的属性的所有元素

问题描述:

我试图选择所有以框"开头的元素.这是我现有的代码:

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'))){

}