2013-11-15 62 views
2

如何從CDATA和描述標籤中獲取圖像? 下面是XML代碼:從CDATA中獲取圖像並使用jquery描述標籤

<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 + '">'); 
+0

除非你是正確配對的XML標記不應該出現在文本中的CDATA。 – Quentin

回答

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

謝謝!這工作!我怎樣才能從描述標籤中刪除CDATA和圖片標籤? – user2811083

+0

我已更新答案 –

0

你必須逃離[ and ]一樣,

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