Javascript 查找<;img>;文本字符串内的标记

Javascript 查找<;img>;文本字符串内的标记,javascript,jquery,xml,parsing,Javascript,Jquery,Xml,Parsing,我有一个xml文档(来自提要),我正在从中提取值: $(feed_data).find("item").each(function() { if(count < 3) { //Pull attributes out of the current item. $(this) is the current item. var title = $(this).find("title").text(); var link = $(this).find

我有一个xml文档(来自提要),我正在从中提取值:

$(feed_data).find("item").each(function() {
    if(count < 3) {
    //Pull attributes out of the current item. $(this) is the current item.
        var title = $(this).find("title").text();
        var link = $(this).find("link").text();
        var description = $(this).find("description").text();
但这是不可能的。img被包裹在一个跨度中,但我也无法达到这个目的。有什么建议吗?我更愿意避免使用子字符串和正则表达式解决方案,但我不知所措

我还尝试将“description”字符串转换为xml对象,如下所示:

var parser = new DOMParser();
var desc = parser.parseFromString(test,'text/xml'); 

$(desc).find("img").each(function() {
    alert("something here");
});

但这也不行。看起来是这样,但我得到了一个“文档格式不正确”错误。

也许您应该尝试将描述文本转换为html标记,然后尝试通过jquery遍历它

$('<div/>').html($(this).find("description").text()).find('img')
$('').html($(this.find(“description”).text()).find('img'))

注意:未测试

尝试将description标记的内容包含在一个虚拟div中,这似乎对我更有效,并允许jQuery的
.find()
按预期工作

e、 g

$(feed_data)。查找(“项”)。每个(函数(){
如果(计数<3){
//从当前项中拉出属性。$(此)是当前项。
var title=$(this.find(“title”).text();
var link=$(this.find(“link”).text();
var description=''+$(this).find(“description”).text()+'';
var image=$(description.find('img');

您好,谢谢您的及时回复。我给了格雷格尔一个勾号,因为我相信他的解决方案会奏效,因为原理与我最终得到的相同。我的解决方案如下所示:

    $(feed_data).find("item").each(function() {
        if(count < 3) {
            //Pull attributes out of the current item. $(this) is the current item.
            var title = $(this).find("title").text();
            var link = $(this).find("link").text();
            var description = $(this).find("description").text();

            var thumbnail = "";
            var temp_container = $("<div></div>");
            temp_container.html(description);
            thumbnail = temp_container.find("img:first").attr("src");
$(feed_data)。查找(“项”)。每个(函数(){
如果(计数<3){
//从当前项中拉出属性。$(此)是当前项。
var title=$(this.find(“title”).text();
var link=$(this.find(“link”).text();
var description=$(this.find(“description”).text();
var=”;
var temp_container=$(“”);
temp_container.html(说明);
缩略图=临时容器.find(“img:first”).attr(“src”);

因此,将字符串包装在div中,然后使用“find()”为了获得第一个IMG元素。现在我有了图像源,它可以根据需要使用。

至少请考虑张贴你正在使用的字符串。否则我们只是试着猜如何帮助。谢谢,这和我最终的解决方案类似,而且我确信这也会起作用。+ 1类似于我的方法,WHI。最终起作用的是ch。
$(feed_data).find("item").each(function() {
    if(count < 3) {
        //Pull attributes out of the current item. $(this) is the current item.
        var title = $(this).find("title").text();
        var link = $(this).find("link").text();
        var description = '<div>' + $(this).find("description").text() + '</div>';
        var image = $(description).find('img');
    $(feed_data).find("item").each(function() {
        if(count < 3) {
            //Pull attributes out of the current item. $(this) is the current item.
            var title = $(this).find("title").text();
            var link = $(this).find("link").text();
            var description = $(this).find("description").text();

            var thumbnail = "";
            var temp_container = $("<div></div>");
            temp_container.html(description);
            thumbnail = temp_container.find("img:first").attr("src");