简体   繁体   中英

How to get all spans in string variable, with particular class name

as i am getting tough time to list out all spans, which having class="ansspans" , may be one or more span with "ansspans" classes will be there, i need to get all the spans with its content and iterate through it. can you tell me how to do it, Regex, Jquery, any thing ok,

  1. The content will be in string variable (not in DOM), as IE 9 ignoring quotes from attribute, so i cant use getelementbyclass name,and i followed this answer, to get quotes InnerHTml workAround , now its displaying with quotes. so i need get all the class of ansspans, in an array, so that i'll iterate it n get the text content of each span

     <span id="sss_ctl00_ctl06_lblanswertext"> assignment <span class="ansspans">submission </span> date : &nbsp;10:07:51 AM </span> 

in this eg, expected output will be 1 span object, so that i can iterate over it

Update : I cant use DOm, as we are in quirks mode, so ie 9 will ignore attribute quotes, which i cant traverse using getelement by class name, . so , i need to match all spans in a string variable. hope everyone understood my problem ;(

The following jQuery selector $('span.ansspans') will get all the <span class="anspans"> for the page.

If you need something for a specific element, add a prefix of the appropriate selector, ie $('#sss_ctl00_ctl06_lblanswertext span.ansspans')

If this needs to be done in a more dynamic way - look into functions like find() , filter() , etc .

(as been said on this site before, sometimes it's ok to parse a limited , known set of xml with regex)

   //assumes no <span id="stupid>" class="ansspans">, and no embedded span.ansspans
        var data = ' <span id="sss_ctl00_ctl06_lblanswertext"> assignment    \n date : &nbsp;10:07:51 AM    \n     <span class="ansspans">ONE has a new \n line in it</span><span class="ansspans">TWO</span><span class="ansspans">3 </span><span class="ansspans">4 </span><span class="ansspans">5 </span>';
        var myregexp = /<span[^>]+?class="ansspans".*?>([\s\S]*?)<\/span>/g;
        var match = myregexp.exec(data);
        var result = "spans found:\n";
        while (match != null) {
            result +=  "match:"+RegExp.$1 + ',\n';
            match = myregexp.exec(data);
        }
        alert(result);

(edited to capture inner html instead of whole tag)

solution using jquery

var tempArray = $("span.ansspans");//this will select all the span elements having class 'ansspans' and return them in an array
var len = tempArray.length;//calculate the length of the array
for(var index = 0;index<len;index++){
    var reqString = $(tempArray[index]).html();
}

These string values can be either put inside an array or can be utilised then and there only.

If you want textContent, use .text() instead of .html()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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