繁体   English   中英

使用JQuery从XML的父节点获取值

[英]Get Value from parent node in XML using JQuery

我试图在选择了程序节点后从父级获取值,以下是ajax函数,该函数加载XML并查找存储在schedule数组中的每个程序ID。

        $.ajax({
                type: "GET",
                url: "tvguide.xml",
                success: function(xml){

                    //for each schedule ID stored in the array this function fires
                    $.each(schedule_array, function(index, item) {
                        //finds the program ID and then inserts the data from the XML into a UL in the Series Details div
                        $(xml).find("programme[id="+item+"]").each(function(){ 
                            //Get the value from the previous node and store here?
                        });
                    });
                },
                error: function(){
                    alert("XML File could not be found.");
                }
            });

以下是XML的一个小示例。

<channel value="channel_1">
        <programme id="1">
            //programmes details in here
        </programme>
 </channel>

基本上,当在上面的循环中选择该通道内的程序时,我需要获取该通道节点的值。 这可能吗?

您可以使用.parent()获取父级引用,使用.attr()获取/设置属性值:

$(xml).find("programme[id="+item+"]").each(function(){
   var parentval = $(this).parent().attr('value');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM