简体   繁体   English

如何获取具有特定类名的字符串变量中的所有跨度

[英]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. 由于我越来越难以列出所有跨度, 而其中的class =“ ansspans”可能是一个或多个带有“ ansspans”类的跨度,因此我需要获取所有跨度及其内容并对其进行迭代。 can you tell me how to do it, Regex, Jquery, any thing ok, 你能告诉我该怎么做吗,正则表达式,jQuery,什么都可以,

  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. 内容将在字符串变量中 (而不是在DOM中),因为IE 9忽略了属性的引号,因此我无法使用getelementbyclass名称,并且我遵循此答案以获取引号InnerHTml workAround ,现在其显示为引号。 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 所以我需要在数组中获取所有的anspans类,以便迭代它n获取每个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 在这种情况下,预期输出将是1个span对象,以便我可以对其进行迭代

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, . 更新:我不能使用DOm,因为我们处于怪癖模式,所以9将忽略属性引号,我不能使用类名getelement遍历属性引号。 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. 以下jQuery选择器$('span.ansspans')将获取页面的所有 <span class="anspans">

If you need something for a specific element, add a prefix of the appropriate selector, ie $('#sss_ctl00_ctl06_lblanswertext span.ansspans') 如果您需要特定元素的内容,请添加适当选择器的前缀,即$('#sss_ctl00_ctl06_lblanswertext span.ansspans')

If this needs to be done in a more dynamic way - look into functions like find() , filter() , etc . 如果需要以更加动态的方式进行,请查看函数find()filter()

(as been said on this site before, sometimes it's ok to parse a limited , known set of xml with regex) (如之前在此站点上所述,有时可以使用regex解析一组有限的 已知 xml)

   //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) (已编辑以捕获内部html而不是整个标签)

solution using jquery 使用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() 如果需要textContent,请使用.text()而不是.html()

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

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