1

I got divs with IDs like that:

<div id="drop_12_213_1">
<div id="drop_12_213_2">
<div id="drop_12_213_3">
<div id="drop_13_213_4">

The only thing I know is drop and the last number(e.g. 1).

How can I select it whit jQuery?

$('div[id^=drop_*_*_1]') is not working?

What am I doing wrong?

TIA frgtv10

4

1 回答 1

3

jQuery doesn't support regular expressions as selectors by default. There are some filters that could do that.

What you can do instead is use the jQuery filter() method.

$('div').filter(function() {
    return this.id.match(/^drop_\d+_\d+_1$/);
}).html("match");

This will basically check all the divs in your document and use the filter() method to see if it should match or not. I imagine this will not be the fastest way of doing, you could try to restrict the search as much as possible (e.g. $('div', container_of_divs)), but by the looks of what you're trying to parse, it seems result is more important than speed.

As a note, your regular expression is wrong too.

drop_*_*_1 translates to something like:

  • drop followed by
  • _ zero or more times,
  • _ zero or more times,
  • _1

Which would match something like drop_1 or drop__1 or drop___..._1.

Links:

于 2012-08-28T12:49:30.090 回答