有哪些方法可以在 Chrome 扩展中选择元素?

发布于 2024-09-13 23:33:33 字数 229 浏览 3 评论 0原文

我了解 getElementById、getElementByNames ... 和 getElementsByTagName

还有哪些其他方法可用于从 DOM 中选择一个或多个元素?如果某个项目没有 ID 或名称怎么办?

我听说我可以使用 XPath 表达式来选择一系列 HTML 元素。我如何使用 JavaScript 来做到这一点?有没有一个图书馆可以帮助我实现这一目标?

提前非常感谢:)

I know about getElementById, getElementByNames ... and getElementsByTagName

What other methods are available to select an element or elements from the DOM ? What if an item does not have an Id or Name.

I have heard that I can use XPath expression to select a range of HTML elements. How do i do that using JavaScript? Is there a library that will help me achieve this?

Thanx a lot in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2024-09-20 23:33:33

为什么不直接使用 jQuery?

如果您在脚本中需要它,您可以通过manifest.json注入jquery.js:

"content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "js": ["jquery.js", "myscript.js"]
    }
  ]

如果您在html页面(背景、选项等)中需要它,您可以使用

<script src="jquery.js"></script>

甚至

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Why not just use jQuery?

If you need it in scripts you can inject jquery.js through manifest.json:

"content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "js": ["jquery.js", "myscript.js"]
    }
  ]

If you need it in html pages (background, options etc) you can use

<script src="jquery.js"></script>

or even

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文