是否有用于标识符(id)的通配符选择器? [英] Is there a wildcard selector for identifiers (id)?

查看:103
本文介绍了是否有用于标识符(id)的通配符选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有数量未知的标识符共享一个特定的命名方案,是否可以使用jQuery一次捕获所有标识符?

If I have an unknown amount of identifiers sharing a specific naming-scheme, is there a way to grab them all at once using jQuery?

// These are the IDs I'd like to select
#instance1
#instance2
#instance3
#instance4

// What do I need to add or how do I need to modify this jQuery selector in order to select all the IDs above?
("#instanceWILDCARD").click(function(){}

推荐答案

属性开始-with选择器('^=)适用于您的ID,如下所示:

The attribute starts-with selector ('^=) will work for your IDs, like this:

$("[id^=instance]").click(function() {
  //do stuff
});

但是,请考虑为您的元素提供一个通用类,例如(我自己搞砸了).instance,并使用该选择器:

However, consider giving your elements a common class, for instance (I crack myself up) .instance, and use that selector:

$(".instance").click(function() {
  //do stuff
});

这篇关于是否有用于标识符(id)的通配符选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆