从xml文件中获取所有img标签(jQuery) [英] Get All img tags out of xml file (jquery)

查看:394
本文介绍了从xml文件中获取所有img标签(jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml文件

<gallery>
<album title="test" description="test" lgPath="/images/commphotos/">
    <img src="1130975173.jpg" />
    <img src="1475634985E.jpg" />
    <img src="1889677107.jpg" />
    <img src="1356256436.jpg" />
    <img src="834682273.jpg" />

</album>
</gallery>

我一直在尝试使用jquery读取它,以获取每个img标签. 最好的方法是什么?

I've been trying to read it using jquery to get out each of the img tags. Whats the best way to do this?

推荐答案

就像对待其他XML文档一样:

Just treat the XML document like any other:

$(xml).find('img');

更新:这将为您提供一个jQuery对象,其中包含所有标记作为DOM元素.例如,如果您希望将标签数组作为字符串,则会变得更加复杂:

Update: This will give you a jQuery object containing all the tags as DOM elements. If you want, say, an array of the tags as strings, it gets more complicated:

var tarr = [];
$(xml).find('img').each(function() {
    tarr.push( $(this).clone().appendTo('<div/>').parent().html() );
});

仅获取src属性:

var tarr = [];
$(xml).find('img').each(function() {
    tarr.push( $(this).attr('src') );
});

这篇关于从xml文件中获取所有img标签(jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆