【程式碼筆記】Web-JavaScript-JavaScript 運算子
阿新 • • 發佈:2018-12-17
一,效果圖。
二,程式碼。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>javascript 運算子</title> </head> <body> <!--javascriptyu運算子--> <p>惦記按鈕計算</p> <button onclick="myFunction0()">點選這裡</button> <p id="demo0"></p> <script> function myFunction0() { y = 5; z = 2; x = y + z; document.getElementById("demo0").innerHTML = x; } </script> <!--用於字串的+運算子--> <p>點選按鈕建立及增加字串變數</p> <button onclick="myFunction()">點選這裡</button> <p id="demo"></p> <script> function myFunction() { txt1 = "What a very"; txt2 = "nice day"; txt3 = txt1 + txt2; document.getElementById("demo").innerHTML = txt3; } </script> <!--在字串中增加空格--> <p>點選按鈕建立及增加字串變數。</p> <button onclick="myFunction1()">點選這裡</button> <p id="demo1"></p> <script> function myFunction1() { txt1 = "What a very "; txt2 = "nice day"; txt3 = txt1 + txt2; document.getElementById("demo1").innerHTML = txt3; } </script> <p>點選按鈕建立及增加字串變數</p> <button onclick="myFunction2()">點選這裡</button> <p id="demo2"></p> <script> function myFunction2() { txt1 = "what a very"; txt2 = "nice day"; txt3 = txt1 + " " + txt2; document.getElementById("demo2").innerHTML = txt3; } </script> <!--對字串和數字進行加法運算--> <P>點選按鈕建立及增加字串變數</p> <button onclick="myFunction()">點選這裡</button> <p id="demo3"></p> <script> function myFunction() { var x = 5 + 5; var y = "5" + 5; var z = "hello" + 5; var demoP = document.getElementById("demo"); demoP.innerHTML = x + "<br>" + y + "<br>" + z; } </script> </body> </html>
參考資料:《菜鳥教程》