js-01-入门
Created At : 2020-08-23 10:07
Count:1.6k
Views 👀 :
前言
JavaScript 与 Java 是两种完全不同的语言,无论在概念上还是设计上。 Java(由 Sun 发明)是更复杂的编程语言。ECMA-262 是 JavaScript 标准的官方名称。JavaScript 由 Brendan Eich 发明。它于 1995 年出现在 Netscape 中(该浏览器已停止更新),并于 1997 年被 ECMA(一个标准协会)采纳。
1. JavaScript引用方式 JavaScript的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!DOCTYPE html > <html > <head > <meta charset ="UTF-8" > <title > JavaScript标识放置<Head > ...</Head > 头部之间</title > <script type ="text/javascript" > var ODiv = document .getElementById("context" ); console .log(ODiv); </script > </head > <body > <div id ="context" > 你好</div > </body > </html >
嵌入式
1 2 3 4 5 6 7 8 <script > javascript语句;</script > <script type ="text/javascript" > javascript语句;</script > ``` - 外链式 ```html <script src ="js/test.js" > </script >
行内式
1 <a href ="javascript:alert'hello';" > test</a >
2. JavaScript 输出
使用 window.alert() 弹出警告框。
使用 document.write() 方法将内容写到 HTML 文档中。
使用 innerHTML 写入到 HTML 元素。
使用 console.log() 写入到浏览器的控制台。
3. 运算符
JavaScript:改变 HTML 内容1 2 3 4 5 6 7 function myFunction() { x=document.getElementById("demo"); // 找到元素 x.innerHTML="Hello JavaScript!"; // 改变内容 } </script > <button type ="button" onclick ="myFunction()" > 点击这里</button >
JavaScript:改变 HTML 图像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <script > alert(220 + 230 ); alert(2 * 3 + 25 / 5 - 1 ); alert(2 * (3 + 25 ) / 5 - 1 ); </script > <script > alert('Hello' ); alert("Hello" ); </script > <script > alert('22' == '22' ); alert('22' == '33' ); </script > <script > alert('220' + '230' ); alert('220 + 230 = ' + 220 + 230 ); alert('220 + 230 = ' + (220 + 230 )); </script > <script > if (22 > 33) { alert(true ); } else { alert(false ); } </script > <script > var num1 = 22 ; var num2 = 33 ; alert(num1 + num2); alert(num1 - num2); </script > <script > var temp = num1; num1 = num2; num2 = temp; alert('num1 = ' + num1 + ', num2 = ' + num2); </script >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <script > window .console; window .document; window .alert('test' ); window .prompt('test' ); </script > <script > window .console.log('test' ); window .document.write('test' ); </script > <body > <div id ="test" > Hello</div > <script > var test = document .getElementById('test' ); alert(test.innerHTML); </script > </body > <script > var str = 'apple' ; alert(str.length); alert(str.toUpperCase()); alert('aa' .toUpperCase()); </script > <script > var stu = {}; stu.name = '小明' ; stu.gender = '男' ; stu.age = 18 ; alert(stu.name); stu.introduce = function ( ) { return '我叫' + this .name + ',今年' + this .age + '岁。' ; }; alert(stu.introduce()); </script >
case01-6 函数的定义与调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <title > 计算1到100个数相加和</title > <script > /* 1:函数的定义 function funName (参数1 , 参数2 , 参数3. ... ) { } 2:函数调用 funName(1,2,3); */ function getSum ( ) { var sum = 0 ; for (var i = 1 ; i <= 100 ; i++) { sum += i; } document .write('1到100个数相加和是:' +sum); } getSum(); </script >
case01-7 自定义函数与调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <script > /* 定义函数 function funName (a,b ) { 函数体 } 在调用函数的时候,传入的参数叫做实参 funName(5, 6); */ function getSum (n, m ) { var sum = 0 ; for (var i = n; i <= m; i++) { sum += i; } console .log(n+'-' +m+'之间的数相加为:' +sum); } getSum(5 , 10 ); getSum(100 ,1000 ); </script >
case01-8 改变网页皮肤
1 2 3 4 5 6 7 8 9 10 <body > <button value ="设为红色" onclick ="color('red')" > 设为红色</button > <button value ="设为黄色" onclick ="color('yellow')" > 设为黄色</button > <button value ="设为蓝色" onclick ="color('blue')" > 设为蓝色</button > <button value ="设为绿色" onclick ="color('green')" > 设为绿色</button > <script type ="text/javascript" > function color (e ) { document .body.style.backgroundColor = e } </script >
case01-9 验证用户名和密码
1 2 3 4 5 6 7 8 9 10 11 12 <script > var userName = prompt("请输入用户名" ); var passWord = prompt("请输入密码" ); if (userName =='admin' && passWord =='123456' ){ alert('登录成功' ) } else { alert('用户名或密码输入错误' ) } </script >
case01-10 输出用户输入的信息
1 2 3 4 5 <script type ="text/javascript" > var msg = prompt("请输入您的信息" ); document .write(msg); </script >
case01-11 计算年龄小游戏
1 2 3 4 5 6 7 8 9 10 11 <input type ="button" onclick ="disp_year()" value ="请输入您的出生年份" /> <script type ="text/javascript" > function disp_year ( ) { var year=prompt("请输入您的出生年份" ,"" ) if (year!=null && year!="" ) { document .write('您今年' +(2019 -year)+'岁' ) } } </script >
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1210331079@qq.com
Title: js-01-入门
Count: 1.6k
Author: 千 羽
Created At: 2020-08-23, 10:07:34
Updated At: 2020-08-23, 10:11:35
Url: https://nateshao.github.io/2020/08/23/js-01-%E5%85%A5%E9%97%A8/
Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.