Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery和XML:这是最后一个元素吗?_Javascript_Jquery_Xml_Ajax - Fatal编程技术网

Javascript jQuery和XML:这是最后一个元素吗?

Javascript jQuery和XML:这是最后一个元素吗?,javascript,jquery,xml,ajax,Javascript,Jquery,Xml,Ajax,我使用这段jQuery获取了一些XML数据: $.ajax({ type: "POST", url: "siteList.xml", dataType: "xml", success: function(xml) { $(xml).find('wrapper').each(function(){ $(this).find('site').each(function(){ //check whe

我使用这段jQuery获取了一些
XML
数据:

$.ajax({
    type: "POST",
    url: "siteList.xml",
    dataType: "xml",
    success: function(xml) {
        $(xml).find('wrapper').each(function(){
            $(this).find('site').each(function(){
                //check whether this is the last element...
            });
        });
    }
});
我想做的是: 检查我是否正在解析我拉入的
XML
中的最后一个
site
元素

编辑: 我不能(据我所知)这样做:

$(this).find('site').each(function(){
    $(this).last(); //nope
    $(this).find('site').last(); //nope
});

请看“:last”选择器或.last()函数。您可以做一个测试,看看当前站点是否与上一个站点相同。

只需使用类似的方法获取最后一个站点:

$(this).find('site').last()

希望有帮助

可能的重复不是重复-这是关于XML不是标记?DOM就是DOM。无论您是从XML还是HTML构建它都无关紧要(至少对于遍历而言)。我的意思是,你也在使用
。在这里查找
。为什么其他函数会有所不同?@Neurofluxation:XML中的“M”代表什么?我不能(据我所知),这样做。。。对,因为您在这里使用
.last
的方式没有意义
$(this).last()
将与
$(this)
相同,因为只选择了一个元素(
this
)。只要
site
没有嵌套,
$(this.find('site')
将返回一个空集。