1

我在 Firefox 扩展中使用它,但无法正常工作。

var allLinks = document.evaluate(
                        '//a[@href]', 
                        window.document, 
                        null, 
                        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, 
                        null);
window.alert(allLinks);

这会发出警报[object XPathResult]。然而,以下总是返回0。(页面上肯定有链接)。

window.alert(allLinks.snapshotLength);
4

3 回答 3

1

如果它是特定于 Firefox 的,并且我假设您只使用新版本..您不能使用 querySelectorAll 吗?

document.querySelectorAll('a[href]')

另外 - 你是在 DOM 完全加载时这样做吗?把它绑定到DOMContentLoaded什么的。

document.addEventListener('DOMContentLoaded', fn, false);

编辑:你的 xpath 对我有用。在现有的已加载页面上的控制台中执行此操作。很确定你在任何东西存在之前就在查询。

于 2010-07-28T19:02:22.833 回答
0

你不想听到的答案——“对我有用”。

请参阅此示例(我的警报 3)。

你在什么浏览器和什么版本上测试这个?

于 2010-07-28T19:06:49.203 回答
0

这行得通。

        var allLinks = top.document.getElementById("content").selectedBrowser.contentDocument.evaluate(
                            treeView.model[0].xpath, 
                            top.document.getElementById("content").selectedBrowser.contentDocument, 
                            null, 
                            XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, 
                            null);
于 2010-07-29T19:23:23.740 回答