get image out of CDATA and description tag with jquery

谁说我不能喝 提交于 2019-12-08 01:08:10

问题


How do i get a image out of a CDATA and description tag? Here is the XML code:

<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>

This is what i got:

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

回答1:


have you try to:

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

to remove the CDATA and image tag out of the description tag :

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



回答2:


You have to escape [ and ] like,

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


来源:https://stackoverflow.com/questions/19998612/get-image-out-of-cdata-and-description-tag-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!