简体   繁体   中英

newtab content cannot be alter using javascript?

I want to alter the content of my newtab using javascript. In my manifest.json I have this

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

then in my index.html I have

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

then in my script.js I do $('h1').remove() nothing triggered. Why? When I try to do console.log($) it worked.

Your code runs as soon as Chrome inserts the <script> tag into the DOM tree.

By the time that happens, <h1> is not there yet .

There are two solutions; one is to simply move the <script> tag below the nodes you refer to.

The other, preferable solution is to use an event that says "okay, DOM tree is complete, you can work with nodes now". Using jQuery, it will be the ready event :

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

Or, shortened,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM