如何使用特定的类名来获取字符串变量中的所有跨度
因为我很难列出所有跨度, 其中包含class =“ansspans” ,可能是一个或多个跨度,“ansspans”类将在那里,我需要获得所有跨度及其内容并迭代它。 你能告诉我怎么做,Regex,Jquery,还有什么好的,
-
内容将在字符串变量 (不在DOM中),因为IE 9忽略属性的引号,所以我不能使用getelementbyclass名称,我按照这个答案,得到引号InnerHTml workAround ,现在它显示引号。 所以我需要在数组中获取所有类的ansspans,以便我将迭代它获取每个跨度的文本内容
assignment submission date : 10:07:51 AM
在这个例子中,预期的输出将是1个span对象,因此我可以迭代它
更新:我不能使用DOm,因为我们处于怪癖模式,所以9将忽略属性引号,我不能通过类名使用getelement遍历。 所以,我需要匹配字符串变量中的所有跨度。 希望大家都明白我的问题;(
(正如之前在这个网站上所说过的,有时可以用正则表达式解析一组有限的 , 已知的xml)
//assumes no , and no embedded span.ansspans var data = ' assignment \n date : 10:07:51 AM \n ONE has a new \n line in itTWO3 4 5 '; var myregexp = /]+?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);
(编辑以捕获内部html而不是整个标记)
以下jQuery选择器$('span.ansspans')
将获取该页面的所有 。
如果您需要某个特定元素的内容,请添加相应选择器的前缀,即$('#sss_ctl00_ctl06_lblanswertext span.ansspans')
如果需要以更动态的方式完成 – 请查看find()
, filter()
等函数。
使用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
这些字符串值既可以放在数组中,也可以在那里使用。
如果你想要textContent,请使用.text()而不是.html()