开发者

Using XPathResult

开发者 https://www.devze.com 2023-01-17 03:06 出处:网络
I\'m finding little documentation on XPathResult on the mozilla developper site. All functions listed redirect to the main page, so they\'re probably not do开发者_开发知识库cumented yet.

I'm finding little documentation on XPathResult on the mozilla developper site. All functions listed redirect to the main page, so they're probably not do开发者_开发知识库cumented yet.

var myFind;
myFind = document.evaluate(
    '/html/body/table[1]',
    document,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null);

I'm looking for a way to alert the HTML tree that is under the path given.

Using alert(myFind); doesn't work, it just gives "XPathResult". There's just a tbody and a bunch of tr elements beneath it, and I'd like to see them all in an alert as 1 string.

What function can myFind use to do this?


var myFind;
myFind = document.evaluate(
    '/html/body/table[1]',
    document,
    null,
    XPathResult.FIRST_ORDERED_NODE_TYPE,
    null);

var node = myFind.singleNodeValue;

I'm using FIRST_ORDERED_NODE_TYPE because you're only looking for a single table. singleNodeValue lets you extract the node.

Now node is a regular HTML DOM Node. You can serialize it the same way as any other node, e.g. with serializeToString:

new XMLSerializer().serializeToString(node)

You may find Using XPath and XPathResult helpful.

0

精彩评论

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