最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery, how to find list of div's with similar ID - Stack Overflow

programmeradmin10浏览0评论

I have a structure of html like this:

<div id="triger1">some elements inside</div>
<div id="triger2">some elements inside</div>
<div id="triger3">some elements inside</div>
<div id="triger4">some elements inside</div>

How do I get array of all the div's in JQuery with the triger ID in them (as you can see, they all have triger but different numbering eg. triger1, triger2 etc...)

I have a structure of html like this:

<div id="triger1">some elements inside</div>
<div id="triger2">some elements inside</div>
<div id="triger3">some elements inside</div>
<div id="triger4">some elements inside</div>

How do I get array of all the div's in JQuery with the triger ID in them (as you can see, they all have triger but different numbering eg. triger1, triger2 etc...)

Share Improve this question edited Dec 24, 2022 at 20:18 starball 49.3k28 gold badges195 silver badges865 bronze badges asked Aug 4, 2009 at 3:25 Dmitri S.Dmitri S. 3,4385 gold badges36 silver badges41 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 116

You can use the following:

$("div[id^='triger']")

This will return all <div> with id starting (^=) with triger.

You can get more information about the various jQuery selectors in the jQuery docs:

API/Selectors

you can actually use a regular expression in a selector to achieve exactly what you are looking for, as such:

$("div:regex(id, yourRegularExpression)");

(Note: this does require the regex-selector plugin)

Someone asked a similar question here.

You can read all about regular expressions here.

As others have pointed out, you can achieve the same result like this:

$("div[id^='partOfID']");

by using a simple jQuery selector. For more complex selections, though, you will need to use the regex plugin or do it manually, as noted in the linked question.

good luck!

Select all div elements whose id attribute contains the string triger:

$('div[id*="triger"]');

There's more about using *= in the jQuery documentation: Attribute Contains Selector [name*="value"]

var trigerList = $("div[id^='triger']");

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论