jquery 正则表达式选择器
我想选择一些名称以 btn (btn1, btn2..) 开头的 html 元素,
的最有效的方法是什么
我尝试 http://james.padolsey.com/javascript/regex-selector-for-jquery/ 但无法使其工作
$(':regex(class,btn[0-9]').hover( function(){
// not selecting any "btn" class
}
I want to select some html elements with name starting with btn (btn1, btn2..)
what is the most efficiant way to do that
I try http://james.padolsey.com/javascript/regex-selector-for-jquery/ but dont manage to make it work
$(':regex(class,btn[0-9]').hover( function(){
// not selecting any "btn" class
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想要选择具有以
btn
开头的class
属性的所有元素:如果您想使用
name
属性,只需交换属性:使用属性值starts-with选择器:http://api.jquery.com/attribute-starts-with-selector/
If you want to select all elements that have a
class
attribute that starts with the wordbtn
:If you want to use the
name
attribute instead, just swap out the attribute:Using the attribute value starts-with selector: http://api.jquery.com/attribute-starts-with-selector/
您正在做的是选择 btnX 类的所有元素,其中 X 可以是 0-9。
要使用以“btn”开头的名称属性执行此操作,您需要使用:
-- 编辑以包含 工作 jsFiddle 演示 -
另外,不要忘记将其放在您的页面上:
What you are doing is selecting all elements with the class of btnX where X could be 0-9.
To do this with the name attribute starting with "btn" you need to use:
-- EDIT to include working jsFiddle demo --
Also, don't forget to put this on your page:
我怀疑正则表达式是最有效的方法,但是您是否将正则表达式选择器添加到代码中?
:regex
不是内置选择器,因此您需要在脚本中包含文章顶部的代码片段。I doubt regex is the most efficient way to do it, but did you add the regex selector to your code?
:regex
is not a built-in selector so you need to include the snippet at the top of your article in your script.