开发者

jQuery method to get one node - rather than find();

开发者 https://www.devze.com 2023-01-14 03:49 出处:网络
I\'ve been Googling this for an hour and can\'t seem to find the answer. I\'m using $(xml).find(\'title\') -- but it seems to be returning all the \"titles\" in all nodes.How do I just grab the title

I've been Googling this for an hour and can't seem to find the answer.

I'm using $(xml).find('title') -- but it seems to be returning all the "titles" in all nodes. How do I just grab the title within the root node?

<response stat="OK">
    <title>Some Document</title>
    <menu>
        <item>
            <title>Some Title</title>
            <url>/</url>
        </item>
        <item>
            <title>Some Title 1</title>
            <url>/asdfasdf/</url>
        </item>
    </menu>
</response>

returns "Some DocumentSome TitleSome Title 1"

// Loads the page content and inserts it into the content area
$.ajax({
    dataType:   'xml',
    url:        'someurl',
    success:    function(data, textStatus, XMLHttpRequest) {

        // returns 
        console.log($(data).find('title').text());


    }

I just want the first title! I would rather not use xpath, i'd rather use the cheapest solu开发者_如何学Pythontion.


You can use .children() instead, like this:

console.log($(data).children('title').text());

.children() only looks at the first level down.

0

精彩评论

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