我有 6 个 html 按钮。我正在使用敲除绑定(bind)并对它们进行不同的索引。他们每个人都有 id 为:
btnFood-0
btnFood-1
btnFood-2
btnFood-3
btnFood-4
btnFood-5
我想唯一地标识点击是否由这些中的任何一个触发?
我试过了,
($(this).attr(id) == btnFood-0) OR ($(this).attr(id) == btnFood-1)
OR ($(this).attr(id) == btnFood-2) OR ($(this).attr(id) == btnFood-3)
OR ($(this).attr(id) == btnFood-4) OR ($(this).attr(id) == btnFood-5)
但这不是一个好的解决方案。我想使用正则表达式之类的东西来识别它来自某些 ($(this).attr(id) == btnFood-*)
来源。
我该怎么做?
最佳答案
你可以只使用indexOf
if ( this.id.indexOf('btnFood-') === 0 ) {
// do stuff
}
或专门针对他们
$('[id^="btnFood-"]').on('click' ...
关于javascript - Jquery:检查属性 id 是否具有特定模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34963339/