admin管理员组

文章数量:1336293

I have a div:

<div id="picture_contents_12356_title"></div>

The '12356' is a unique id that is auto-generated and I need to have it there. How can I create a jQuery selector for an id that has "picture_contents" AND "title"?

I know you can do [id*="picture_contents"] but I also need to check if it contains "title" as well. How do I do create the AND logic there?

I was thinking maybe extracting the id into a variable and checking indexOf("picture_contents") and indexOf("title") but I was wondering if there was a better way.

I have a div:

<div id="picture_contents_12356_title"></div>

The '12356' is a unique id that is auto-generated and I need to have it there. How can I create a jQuery selector for an id that has "picture_contents" AND "title"?

I know you can do [id*="picture_contents"] but I also need to check if it contains "title" as well. How do I do create the AND logic there?

I was thinking maybe extracting the id into a variable and checking indexOf("picture_contents") and indexOf("title") but I was wondering if there was a better way.

Share edited Apr 6, 2015 at 18:09 Joe 15.6k8 gold badges50 silver badges57 bronze badges asked Apr 6, 2015 at 17:54 AlistairAlistair 6411 gold badge7 silver badges22 bronze badges 1
  • 7 $('[id*="picture_contents"][id*="title"]') – Shaunak D Commented Apr 6, 2015 at 17:56
Add a ment  | 

2 Answers 2

Reset to default 9

Just keep adding attribute based selectors [attribute*=".."]

In your case,

$('[id*="picture_contents"][id*="title"]')

Suggestion: if the pattern of words is constant, you could use ^= : starts-with or $=: ends-with selector too.


For,

<div id="picture_contents_12356_title"></div>

Use,

$('[id^="picture_contents"][id$="title"]')

Alternatively you could give all of the appropriate divs a class and refer to that instead.

本文标签: javascriptHow to check if id contains multiple wordsStack Overflow