gpt4 book ai didi

javascript - 为什么 document.evaluate 不起作用?

转载 作者:行者123 更新时间:2023-11-28 02:53:02 25 4
gpt4 key购买 nike

我在 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);

最佳答案

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

document.querySelectorAll('a[href]')

另外 - 你是在 DOM 完全加载时执行此操作吗?将其绑定(bind)到 DOMContentLoaded 或其他东西。

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

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

关于javascript - 为什么 document.evaluate 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3356387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com