简体   繁体   English

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

[英]Find element containing specific text and certain parents

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. 我想做的是取一个名字,例如rgoulet,并检索分配给他的任务的标题和/或ID。 So the first goal is to search my jQuery object $(data) for any owner element with the text "rgoulet". 因此,第一个目标是在我的jQuery对象$(data)中搜索带有文本“ rgoulet”的任何所有者元素。 Next I want to get the title and id directly above it. 接下来,我想直接在其上方获得标题和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为所有者的每个实例搜索$(data)的方法,但是所有方法都以描述性的“解析错误”结尾。 :) :)

Thanks in advance! 提前致谢!

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

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

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

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