开发者

Find element containing specific text and certain parents

开发者 https://www.devze.com 2023-02-09 10:58 出处:网络
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:

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:

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:

<tasks>
    <task>
        <title>Refresh Sports page code base</title>
        <id>15276645606996729654</id>
        <owners>
            <owner>
                bfranklin
            </owner>
            <owner>
                bting
            </owner>
        </owners>
    <开发者_JAVA百科;/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.

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". :)

Thanks in advance!


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号