简体   繁体   English

如何检查id是否包含多个单词?

[英]How to check if id contains multiple words?

I have a div: 我有一个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. '12356'是一个自动生成的唯一ID,我需要在那里拥有它。 How can I create a jQuery selector for an id that has "picture_contents" AND "title"? 如何为具有“picture_contents”和“title”的id创建jQuery选择器?

I know you can do [id*="picture_contents"] but I also need to check if it contains "title" as well. 我知道你可以做[id*="picture_contents"]但我也需要检查它是否包含“标题”。 How do I do create the AND logic there? 如何在那里创建AND逻辑?

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. 我想也许可以将id提取到变量中并检查indexOf("picture_contents")indexOf("title")但我想知道是否有更好的方法。

Just keep adding attribute based selectors [attribute*=".."] 只需继续添加基于属性的选择器[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. 建议:如果单词的模式是常量,你可以使用^= :starts-with或$= :ends-with selector。


For, 对于,

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

Use, 采用,

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

或者,你可以给所有适当的div一个类,并参考它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM