无法使用 javascript 更改新标签内容?

newtab content cannot be alter using javascript?

我想使用 javascript 更改我的新标签页的内容。在我的 manifest.json 我有这个

"chrome_url_overrides": {
      "newtab": "index.html"
   }

然后在我的 index.html 我有

<script src="jquery.js"></script>
<script src="script.js"></script>
<body>
<h1>hello world</h1>
</body>

然后在我的 script.js 中我 $('h1').remove() 没有触发。为什么?当我尝试做 console.log($) 时,它成功了。

只要 Chrome 将 <script> 标记插入 DOM 树,您的代码就会运行。

到那个时候,<h1> 还没有

有两种解决方法;一种是简单地将 <script> 标签移动到您引用的节点下方。

另一种更好的解决方案是使用一个表示 "okay, DOM tree is complete, you can work with nodes now" 的事件。使用 jQuery,它将是 the ready event:

$(document).ready(function() {
  /* your code here */
});

或者,缩短,

$(function() {
  /* your code here */
});