.load上的Jquery在Chrome扩展中不起作用,即使使用本地jquery副本[重复]

dauxcl2d  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(135)

此问题已在此处有答案

In a Chrome Extension content script, must I wait for document.ready before processing the document?(2个答案)
8天前关闭
我正在开发一个Chrome扩展程序,我有以下文件。
但是当我打开https://example.com/时,h1标题没有更新。
我在开发控制台中没有看到任何错误。我错过了什么?
manifest.json

  1. {
  2. "manifest_version": 3,
  3. "name": "Print Hello",
  4. "version": "1.0",
  5. "icons": {
  6. "48": "images/icon-48.png"
  7. },
  8. "action": {
  9. "default_icon": {
  10. "48": "images/icon-48.png"
  11. },
  12. "default_title": "Print Hello"
  13. },
  14. "content_scripts": [
  15. {
  16. "matches": ["https://example.com/"],
  17. "js": ["./scripts/jquery.min.js", "./scripts/print_hello.js"]
  18. }
  19. ],
  20. "host_permissions": [
  21. "https://example.com/"
  22. ]
  23. }

print_hello.js

  1. function print_hello() {
  2. document.querySelector("div h1").innerText = "Hello!"
  3. }
  4. $(document).on("load", print_hello)
ncgqoxb0

ncgqoxb01#

你能试着在文档准备好的情况下修改你的代码吗?
所以print_hello.js将是

  1. function print_hello() {
  2. document.querySelector("div h1").innerText = "Hello!"
  3. }
  4. $(document ).ready( print_hello);
  5. // or
  6. // $( window ).on( "load", print_hello );
  7. // instead of document `window` for accessing the the document ready.

相关问题