查找包含特定文本和某些父项的元素

时间:2022-02-12 20:33:22

I've searched up and down but cannot find what I am looking for.

我上下搜索但找不到我要找的东西。

I am pulling data using a pretty basic jQuery.ajax call:

我使用一个非常基本的jQuery.ajax调用来提取数据:

function retrieveFeed() {
    $.ajax({
        type: "GET",
        url: "http://xxxxx/taskdata.xml",
        beforeSend: function(XMLHttpRequest) {
        return true;
        },
        timeout: 5000,
        dataType: "xml",
        success: function(data) {
            parseXML(data);
        },

        error: function() {
        console.log("Error retrieving XML");
        }
    });
}

The XML is really simple:

XML非常简单:

<tasks>
    <task>
        <title>Refresh Sports page code base</title>
        <id>15276645606996729654</id>
        <owners>
            <owner>
                bfranklin
            </owner>
            <owner>
                bting
            </owner>
        </owners>
    </task>
    <task>
        <title>Security audit for college sports pages</title>
        <id>12019410900674133028</id>
        <owners>
            <owner>
                rgoulet
            </owner>
            <owner>
                bfranklin
            </owner>
        </owners>
    </task>
    <task>
        <title>Fill holes in site monitoring</title>
        <id>828664522750709776</id>
        <owners>
            <owner>
                rgoulet
            </owner>
            <owner>
                dgreene
            </owner>
            <owner>
                lkeene
            </owner>
        </owners>
    </task>
</tasks>

What I am trying to do is take a name, say rgoulet, and retrieve the title and/or id of the tasks assigned to him. So the first goal is to search my jQuery object $(data) for any owner element with the text "rgoulet". Next I want to get the title and id directly above it.

我想要做的是取一个名字,比如rgoulet,并检索分配给他的任务的标题和/或id。所以第一个目标是使用文本“rgoulet”搜索我的jQuery对象$(data)中的任何所有者元素。接下来我想直接在它上面获得标题和ID。

I know this has to be possible but I'm stumped. I've seen "parents" but I don't know how to specify. Plus, I have tried a few ways of searching $(data) for every instance of owner with rgoulet but all have ended up in the all-so-descriptive "parse error". :)

我知道这必须是可能的,但我很难过。我见过“父母”,但我不知道如何指明。另外,我尝试了几种使用rgoulet为每个所有者实例搜索$(数据)的方法,但所有这些方法都以最具描述性的“解析错误”结束。 :)

Thanks in advance!

提前致谢!

1 个解决方案

#1


1  

Once you've found the correct <owner> containing 'rgoulet', use .closest('task') to find the nearest parent element of type <task>:

找到包含'rgoulet'的正确 后,使用.closest('task')查找 类型最近的父元素:

var $task = $(data).find("owner:contains('rgoulet')").closest('task');

#1


1  

Once you've found the correct <owner> containing 'rgoulet', use .closest('task') to find the nearest parent element of type <task>:

找到包含'rgoulet'的正确 后,使用.closest('task')查找 类型最近的父元素:

var $task = $(data).find("owner:contains('rgoulet')").closest('task');