1、把js变量、语句、表达式例子练习一遍
2、函数定义及调用、匿名函数练习一遍
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js变量</title> </head> <body> <h1>JS变量</h1> <p id="demo"></p> <script> var price1 = 1; var price2 = 2; var price3 = 3; var total = price1 + price2 + price3; // document.getElementById("demo").innerHTML = "总计:" + total; document.getElementById('demo').innerHTML = '总计:' + total; </script> <hr> <h1>JS语句</h1> <p id="demo1"></p> <script> var name= "我是谁?"; document.getElementById('demo1').innerHTML = name; </script> <hr> <h2>JavaScript 函数是通过 function 关键词来定义的</h2> <h2>调用</h2> <p id="demo2"></p> <script> var x = sum(4,2); function sum(a,b) { return a * b; } document.getElementById('demo2').innerHTML = x; </script> <hr> <h3>匿名调用:没有实际名字的函数</h3> <p id="demo3"></p> <script> (function () { console.log('你好'); })(); </script> <hr> <h5>JavaScript 弹窗</h5> <button onclick="myFunction()">试一试</button> <p id="demo4"></p> <script> function myFunction(){ var text; if (alert("Press a button!")){ text = "您点击了确定"; } else { text = "您点击了取消按钮"; } document.getElementById("demo4").innerHTML = text; } </script> </body> </html>