Is there a wildcard selector for identifiers (id)?

前端 未结 9 1655
臣服心动
臣服心动 2020-12-04 14:59

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          


        
9条回答
  •  不思量自难忘°
    2020-12-04 15:35

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

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

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

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

提交回复
热议问题