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

javascript - Iterate through elements with similar IDs - Stack Overflow

programmeradmin2浏览0评论

I want to get the id of a single <span> element during iteration.

Example scenario:

I have a few ids which start with AAA and end with BBB. The middle characters can vary:

 <span id="AAA-LS-BBB">hi1</span>
 <span id="AAA-LAS-BBB">hi2</span>
 <span id="AAA-LBS-BBB">hi3</span>
 <span id="AAA--LCS--BBB">hi4</span>

Here, first 3 digits of span id and last 3 digits of span id is same...only in middle it will vary. I want to generalized method to know ...each id using... $("#id").attr("id") so, here I am trying to find out iteration like below, I want to write one iterator method to get the current id

$("span[id^='AAA-***-BBB']").each(function(){
    var a = $("this").attr("id");
});

Hope my requirements are clear. how ti use regular expression here.. to find out id?

I want to get the id of a single <span> element during iteration.

Example scenario:

I have a few ids which start with AAA and end with BBB. The middle characters can vary:

 <span id="AAA-LS-BBB">hi1</span>
 <span id="AAA-LAS-BBB">hi2</span>
 <span id="AAA-LBS-BBB">hi3</span>
 <span id="AAA--LCS--BBB">hi4</span>

Here, first 3 digits of span id and last 3 digits of span id is same...only in middle it will vary. I want to generalized method to know ...each id using... $("#id").attr("id") so, here I am trying to find out iteration like below, I want to write one iterator method to get the current id

$("span[id^='AAA-***-BBB']").each(function(){
    var a = $("this").attr("id");
});

Hope my requirements are clear. how ti use regular expression here.. to find out id?

Share Improve this question edited Apr 5, 2011 at 4:56 pradeep cs asked Apr 5, 2011 at 4:33 pradeep cspradeep cs 5234 gold badges9 silver badges35 bronze badges 2
  • What do you mean with print? How? Where? – emco Commented Apr 5, 2011 at 4:37
  • Uhh, quite unclear I must say. Either use [jsfiddle](jsfiddle) or update your post with at the very least some HTML and a clearer description of what it is you are trying to achieve. – Khez Commented Apr 5, 2011 at 4:38
Add a ment  | 

3 Answers 3

Reset to default 9

Use .filter() bined with a regex:

$('span[id]').filter(function () {
    return /^AAA.*BBB$/.test(this.id);
}).each(function () {
    alert(this.id);
});

Alternatively, since you are using a .each() already, you can simply add an if condition inside it:

$('span[id]').each(function () {
    if (/^AAA.*BBB$/.test(this.id)) {
        alert(this.id);
    }
});

Update: It looks like you can also bine both the Attribute-Starts-With and Attribute-Ends-With selectors on the same attribute:

$('div[id^="AAA"][id$="BBB"]').each(function () {
    alert(this.id);
});

(See example: http://jsfiddle/2pz8X/)

Combining fellow contributors' suggestions, and making the regex more specific:

// first, at least get all <div>'s with id's that start with 'AAA'
$("div[id^='AAA']").filter(function(){ 
    var regexp = /^A{3}(.*)B{3}$/; // regex
    return (this.id.match(regexp));
});

This will return all the div's that you need to target.

Sample @ http://jsfiddle/4ZNWJ/

Try this:

$("span[id^='AAA']").each(function(){
  alert($(this).id);
});
发布评论

评论列表(0)

  1. 暂无评论