开发者

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>&lt;ul&gt;&lt;li&gt;The content text&lt;/li&gt;&lt;/ul&gt;</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(/&lt;/g,'<')
               .replace(/&gt;/g,'>')
               .replace(/&amp;/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++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜