可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Im getting a error in the Web Inspector as shown below:
TypeError: 'null' is not an object (evaluating 'myButton.onclick = function() { var userName = myTextfield.value; greetUser(userName); return false; }')
Here is my Code (HTML):
<h2>Hello World!</h2> <p id="myParagraph">This is an example website</p> <script src="js/script.js" type="text/javascript"></script> <form> <input type="text" id="myTextfield" placeholder="Type your name" /> <input type="submit" id="myButton" value="Go" /> </form>
Here is the JS:
var myButton = document.getElementById("myButton"); var myTextfield = document.getElementById("myTextfield"); function greetUser(userName) { var greeting = "Hello " + userName + "!"; document.getElementsByTagName ("h2")[0].innerHTML = greeting; } myButton.onclick = function() { var userName = myTextfield.value; greetUser(userName); return false; }
Any Idea why I am getting the error?
回答1:
Put the code so it executes after the elements are defined, either with a DOM ready callback or place the source under the elements in the HTML.
document.getElementById()
returns null
if the element couldn't be found. Property assignment can only occur on objects. null
is not an object (contrary to what typeof
says).
回答2:
Any JS code which executes and deals with DOM elements should execute after the DOM elements have been created. JS code is interpreted from top to down as layed out in the HTML. So, if there is a tag before the DOM elements, the JS code within script tag will execute as the browser parses the HTML page.
So, in your case, you can put your DOM interacting code inside a function so that only function is defined but not executed.
Then you can add an event listener for document load to execute the function.
That will give you something like:
<script> function init() { var myButton = document.getElementById("myButton"); var myTextfield = document.getElementById("myTextfield"); myButton.onclick = function() { var userName = myTextfield.value; greetUser(userName); } } function greetUser(userName) { var greeting = "Hello " + userName + "!"; document.getElementsByTagName ("h2")[0].innerHTML = greeting; } document.addEventListener('readystatechange', function() { if (document.readyState === "complete") { init(); } }); </script> <h2>Hello World!</h2> <p id="myParagraph">This is an example website</p> <form> <input type="text" id="myTextfield" placeholder="Type your name" /> <input type="button" id="myButton" value="Go" /> </form>
Fiddle at - http://jsfiddle.net/poonia/qQMEg/4/
回答3:
I think the error because the elements are undefined ,so you need to add window.onload
event which this event will defined your elements when the window is loaded.
window.addEventListener('load',Loaded,false); function Loaded(){ var myButton = document.getElementById("myButton"); var myTextfield = document.getElementById("myTextfield"); function greetUser(userName) { var greeting = "Hello " + userName + "!"; document.getElementsByTagName ("h2")[0].innerHTML = greeting; } myButton.onclick = function() { var userName = myTextfield.value; greetUser(userName); return false; } }
回答4:
Try loading your javascript after.
Try this:
<h2>Hello World!</h2> <p id="myParagraph">This is an example website</p> <form> <input type="text" id="myTextfield" placeholder="Type your name" /> <input type="submit" id="myButton" value="Go" /> </form> <script src="js/script.js" type="text/javascript"></script>
回答5:
I agree with alex about making sure the DOM is loaded. I also think that the submit button will trigger a refresh.
This is what I would do
<html> <head> <title>webpage</title> </head> <script type="text/javascript"> var myButton; var myTextfield; function setup() { myButton = document.getElementById("myButton"); myTextfield = document.getElementById("myTextfield"); myButton.onclick = function() { var userName = myTextfield.value; greetUser(userName); return false; } } function greetUser(userName) { var greeting = "Hello " + userName + "!"; document.getElementsByTagName("h2")[0].innerHTML = greeting; } </script> <body onload="setup()"> <h2>Hello World!</h2> <p id="myParagraph">This is an example website</p> <form> <input type="text" id="myTextfield" placeholder="Type your name" /> <input type="button" id="myButton" value="Go" /> </form> </body> </html>
have fun!