使用jquery从CDATA和描述标记中获取图像

时间:2013-11-15 10:13:37

标签: javascript jquery xml rss

如何从CDATA和描述标签中获取图像? 这是XML代码:

<description>
    <![CDATA[
        <img src='http://www.autowereld.com/imagesDB/100/311151018460_.jpg' border='0'                     bordercolor='black' style='float:left; margin-right: 8px;margin-bottom: 8px;'>
    ]]>

    Het is Subaru wat de klok slaat deze week. Na spyshots van de Subaru Legacy Touring,     gelekte foto’s van de productierijpe WRX en de officiële bekendmaking van de Legacy Concept is     het nu tijd voor de Crossover 7 Concept.
</description>

这就是我得到的:

var img = $(element).find("description").text();
img = img.replace("<![CDATA[", "").replace("]]>", "");
console.log(img);
$("#img").append('<img src="' + img + '">');

2 个答案:

答案 0 :(得分:1)

你试着:

var img = $(element).find("description").text();
        img = img.replace("<![CDATA[", "").replace("]]>", "");
        console.log(img);
        $("#img").append(img); // <-- use only the var ing

从描述标记中删除CDATA和图像标记:

var dsr = $(element).find("description").text();
dsr= dsr.substring(dsr.indexOf('>')+1, dsr.length);

答案 1 :(得分:0)

你必须逃避[ and ]之类的,

img = img.replace("<!\[CDATA\[", "").replace("\]\]>", "");
相关问题