Parsing content in array
I would need to get only the text from certain array. So I need to parse it. I have the following code:
for(var x=0;x<contentArray.length;x++){
markup += '<td>'+contentArray[x+1] +'</td>';
x++;
}
Which gives the following output:
<td><c><ul><li>The content text</li></ul></c></td>
And it loks on browser like this:
<ul><li>The content text</li></ul>
Now I would like开发者_C百科 to get only the text (The content text& in this case). How am I able to do tit?
You can use this function to unescape HTML (stolen from the Prototype source code):
function unescapeHTML(html) {
return html
.replace(/</g,'<')
.replace(/>/g,'>')
.replace(/&/g,'&');
}
You can then use jQuery's parsing capability to get the text from the tags:
for(var x=0;x<contentArray.length;x++){
var $el = $(unescapeHTML(contentArray[x+1])).find('li'); //use the unescaped HTML to construct a jQuery object and find the li tag within it
markup += '<td>' + $el.text() + '</td>'; // get the text from the jQuery object and insert it into the fragment
x++;
}
精彩评论