问题
So, I am trying to do a basic Google Chrome Hello World kind of extension. Can someone explain me why the below code doesn't work? Thanks.
popup.js:
document.getElementById("foobar").innerHTML = "Hello Chrome Extensions";
popup.html:
<!doctype html>
<html>
<head>
<title>Hello Chrome</title>
<script src="popup.js"></script>
<div id="foobar"></div>
</head>
<body>
</body>
</html>
I am following the "framework" of http://developer.chrome.com/extensions/getstarted.html.
回答1:
It can be solved in two ways:
- Swap the order of
<script .. >
and<div ..>
. Wrap the code in
popup.js
in a domready event:document.addEventListener('DOMContentLoaded', function() { // Code here... });
Your code failed because the <div>
was unknown at the time of executing the script.
来源:https://stackoverflow.com/questions/15325447/chrome-extension-chaing-content-from-the-javascript