JavaScript output
JavaScript can output data in 4 different ways:
Use window.alert() to pop up an alert box.
Use the document.write() method to write the content into the HTML document.
Use innerHTML to write to HTML elements.
Use console.log() to write to the browser's console.
#window.alert()
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(php.cn)</title> <script src="/static/js/abc.js"></script><script> function myFunction() { window.alert("Hello! 這是一個按鈕"); } </script> </head> <body> <input type="button" onclick="myFunction()" value="Show alert box"> </body> </html>
document.write()
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(php.cn)</title> </head> <body> <h1>Head</h1> <script> document.write('<p>hello document</p>'); </script> <h2>Tail</h2> </html>
innerHTML
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(php.cn)</title> <script type="text/javascript"> function getInnerHTML(){ alert(document.getElementById("test").innerHTML); } </script> </head> <body> <p id="test"><span color="#000">php中文網</span></p> <input type="button" onclick="getInnerHTML()" value="點擊" /> </body> </html>
console.log()
If your browser supports debugging, you can use the console.log() method Display JavaScript value in browser. Use F12 in the browser to enable debugging mode, and click the "Console" menu in the debugging window.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(php.cn)</title> <script type="text/javascript"> a = 5; b = 6; c = a + b; console.log(c); </script> </head> <body> <p>在瀏覽器中使用F12來調試,在調試窗口中點擊 "Console"</p> </body> </html>