• JavaScript


    一、什么是JavaScript

    二、JS的引入方式

    1.内部脚本

    2.外部脚本

     3.代码

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script src="JS/JS.demo.js">script>
    10. body>
    11. html>

    三、JS基础语法

    1.书写语法

    2.输出语句

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. // 方式一:弹出警告框
    11. window.alert("Hello JS")
    12. // 方式二:写入html 页面中
    13. document.write("Hello JS")
    14. // 方式三:控制台输出
    15. console.log("Hello JS")
    16. script>
    17. body>
    18. html>

    3. 变量定义

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. // 变量定义
    11. // var a=13
    12. // alert(a)
    13. // a="张三"
    14. // alert(a)
    15. // 特点1:作用域比较大,全局变量
    16. // 特点2:可以重复定义
    17. // {
    18. // var x=666
    19. // var x=777
    20. // }
    21. // alert(x)
    22. //let:定义局部变量,代码块外无效
    23. // {
    24. // let a=66;//不能重复定义
    25. // alert(a);
    26. // }
    27. // const:常量不能被改变 ,否则会报错
    28. const pi=3.14;
    29. alert(pi);
    30. script>
    31. body>
    32. html>

    4.数据类型,运算符,流程控制语句

    1.数据类型

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. alert(typeof 3.14);//number
    11. alert(typeof 3);//number
    12. alert(typeof "A");//String
    13. alert(typeof "Hello");//String
    14. alert(typeof true);//boolean
    15. alert(typeof false);//boolean
    16. alert(typeof null);//object
    17. var a;
    18. alert(typeof a);//undefined
    19. script>
    20. body>
    21. html>

    2.运算符

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. // alert(typeof 3.14);//number
    11. // alert(typeof 3);//number
    12. // alert(typeof "A");//String
    13. // alert(typeof "Hello");//String
    14. // alert(typeof true);//boolean
    15. // alert(typeof false);//boolean
    16. // alert(typeof null);//object
    17. // var a;
    18. // alert(typeof a);//undefined
    19. // 类型转换:其他类型转换为数字类型
    20. // alert(parseInt ("12"))//12
    21. // alert(parseInt ("12A12"))//12
    22. // alert(parseInt ("A45"))//NaN(not a number)
    23. //类型转换:数字类型转换为boolean类型
    24. // if(0){//false
    25. // alert("0转换为false")
    26. // }
    27. // if(NaN){//false
    28. // alert("NaN转换为false")
    29. // }
    30. // if(-1){//true
    31. // alert("初0和NaN其他数字转为True")
    32. // }
    33. //类型转换:字符串类型转换为boolean类型
    34. if(""){
    35. alert("空字符串为false,其他都是true")
    36. }
    37. if(null){
    38. alert("null转换为false")
    39. }
    40. if(undefined){
    41. alert("undefined转换为false")
    42. }
    43. script>
    44. body>
    45. html>

    3.流程控制语句

     四、JS函数

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. //定义函数 方式:1
    11. // function add(a,b){
    12. // return a+b;
    13. // }
    14. //定义函数 方式:2
    15. var add=function (a,b){
    16. return a+b;
    17. }
    18. //函数调用
    19. var result=add(10,20);
    20. alert(result);
    21. script>
    22. body>
    23. html>

    五、JS中的对象

    1.Array数组

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. //定义数组方式1
    11. // var arr=new Array(1,2,3,4);
    12. //定义数组方式2
    13. // var arr =[1,2,3,4]
    14. // console.log(arr[0]);
    15. // console.log(arr[1]);
    16. //特点 :长度可变 类型可变
    17. // var arr =[1,2,3,4]
    18. // arr[10]=50;
    19. // console.log(arr[10]);
    20. // console.log(arr[9]);//undefined
    21. // console.log(arr[8]);//undefined
    22. // arr[9]="A";
    23. // arr[8]=true;
    24. // console.log(arr);
    25. var arr =[1,2,3,4]
    26. // arr[10]=100;
    27. // for (let i = 0; i < arr.length; i++) {
    28. // console.log(arr[i]);
    29. // }
    30. // console.log("====================================");
    31. // //forEach 遍历数组中有值的元素
    32. // arr.forEach(function (e) {
    33. // console.log(e);
    34. // })
    35. // //箭头函数(...)=>{...}简化
    36. // arr.forEach( (e)=> {
    37. // console.log(e);
    38. // })
    39. arr.push(7,8,9);
    40. // console.log(arr);
    41. arr.splice(2,2);
    42. console.log(arr);
    43. script>
    44. body>
    45. html>

    2.String对象

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. //创建字符串对象
    11. // var str=new String("Hello String");
    12. var str=" Hello String ";
    13. console.log(str);
    14. //length
    15. console.log(str.length);
    16. //charAt
    17. console.log(str.charAt(3));
    18. console.log(str.charAt(4));
    19. //indexof
    20. console.log(str.indexOf("lo"));
    21. //trim
    22. var s=str.trim()
    23. console.log(s);
    24. //substring(start,end)---开始索引,结束索引(含头不含尾)
    25. console.log(s.substring(0,5));
    26. script>
    27. body>
    28. html>

    3.JSON对象

    1.自定义对象

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. //自定义对象
    11. var user={
    12. name:"Tom",
    13. age:10,
    14. gender:"male",
    15. // eat: function(){
    16. // alert("用膳");
    17. // }
    18. eat(){
    19. alert("用膳");
    20. }
    21. }
    22. alert(user.name);
    23. user.eat();
    24. script>
    25. body>
    26. html>

    2.JSON对象

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. // //自定义对象
    11. // var user={
    12. // name:"Tom",
    13. // age:10,
    14. // gender:"male",
    15. // // eat: function(){
    16. // // alert("用膳");
    17. // // }
    18. // eat(){
    19. // alert("用膳");
    20. // }
    21. // }
    22. // alert(user.name);
    23. // user.eat();
    24. //定义JSON
    25. var jsonstr='{"name":"Tom","age":18,"addr":["北京","上海","西安"]}';
    26. alert(jsonstr);
    27. //json字符串--js对象
    28. var s =JSON.parse(jsonstr);
    29. alert(s.name);
    30. //js对象 ---字符串
    31. var str=JSON.stringify(s);
    32. alert(str);
    33. script>
    34. body>
    35. html>

    4.BOM对象

    1.浏览窗口对象

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. //获取
    11. // window.alert("hello BOM");
    12. // alert("BOM");
    13. //方法
    14. //confirm -对话框-确认:true,取消:false
    15. // var flag=confirm("您却仍删除该记录吗?");
    16. // alert(flag);
    17. // 定时器 -setInterval--周期性的执行某一个函数
    18. // var i=0;
    19. // setInterval(function(){
    20. // i++;
    21. // console.log("定时器执行了"+i+"次");
    22. // },2000);
    23. //定时器-setTimeout --延迟指定时间执行一次
    24. setTimeout(function(){
    25. alert("JS")
    26. },3000);
    27. script>
    28. body>
    29. html>

    2.地址栏对象

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <script>
    10. //获取
    11. // window.alert("hello BOM");
    12. // alert("BOM");
    13. //方法
    14. //confirm -对话框-确认:true,取消:false
    15. // var flag=confirm("您却仍删除该记录吗?");
    16. // alert(flag);
    17. // 定时器 -setInterval--周期性的执行某一个函数
    18. // var i=0;
    19. // setInterval(function(){
    20. // i++;
    21. // console.log("定时器执行了"+i+"次");
    22. // },2000);
    23. //定时器-setTimeout --延迟指定时间执行一次
    24. // setTimeout(function(){
    25. // alert("JS")
    26. // },3000);
    27. //location
    28. alert(location.href);
    29. location.href="https://www.baidu.com";//跳转到百度
    30. script>
    31. body>
    32. html>

    5.DOM对象

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>JS-对象-DOM演示title>
    8. head>
    9. <body>
    10. <div style="font-size: 30px; text-align: center;" id="tb1">课程表div>
    11. <table width="800px" border="1" cellspacing="0" align="center">
    12. <tr>
    13. <th>学号th>
    14. <th>姓名th>
    15. <th>分数th>
    16. <th>评语th>
    17. tr>
    18. <tr align="center" class="data">
    19. <td>001td>
    20. <td>张三td>
    21. <td>90td>
    22. <td>很优秀td>
    23. tr>
    24. <tr align="center" class="data">
    25. <td>002td>
    26. <td>李四td>
    27. <td>92td>
    28. <td>优秀td>
    29. tr>
    30. <tr align="center" class="data">
    31. <td>003td>
    32. <td>王五td>
    33. <td>83td>
    34. <td>很努力,不错td>
    35. tr>
    36. <tr align="center" class="data">
    37. <td>004td>
    38. <td>赵六td>
    39. <td>98td>
    40. <td>666td>
    41. tr>
    42. table>
    43. <br>
    44. <div style="text-align: center;">
    45. <input id="b1" type="button" value="改变标题内容" onclick="fn1()">
    46. <input id="b2" type="button" value="改变标题颜色" onclick="fn2()">
    47. <input id="b3" type="button" value="删除最后一个" onclick="fn3()">
    48. div>
    49. body>
    50. <script>
    51. function fn1(){
    52. document.getElementById('tb1').innerHTML="学员成绩表";
    53. }
    54. function fn2(){
    55. document.getElementById('tb1').style="font-size: 30px; text-align: center; color: red;"
    56. }
    57. function fn3(){
    58. var trs = document.getElementsByClassName('data');
    59. trs[trs.length-1].remove();
    60. }
    61. script>
    62. html>

    1.使用DOM获取元素

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <img src="img/off.gif" id="h1"><br><br>
    10. <div class="cls">船只教育div><br>
    11. <div class="cls">黑马程序员div><br>
    12. <input type="checkbox" name="hobby">电影
    13. <input type="checkbox" name="hobby">旅游
    14. <input type="checkbox" name="hobby">游戏
    15. <script>
    16. //1.获取Element元素
    17. //1.1获取元素-根据ID获取
    18. // var img=document.getElementById("h1");
    19. // alert(img);
    20. //1.2获取元素-根据标签获取-div
    21. // var divs =document.getElementsByTagName("div")
    22. // for (let i = 0; i < divs.length; i++) {
    23. // alert(divs[i]);
    24. // }
    25. //1.3获取元素-根据name属性获取
    26. // var ins =document.getElementsByName("hobby");
    27. // for (let i = 0; i < ins.length; i++) {
    28. // alert(ins[i]);
    29. // }
    30. //1.4获取元素-根据class属性获取
    31. // var divs= document.getElementsByClassName("cls");
    32. // for (let i = 0; i < divs.length; i++) {
    33. // alert(divs[i]);
    34. // }
    35. //2查询参考手册,属性方法
    36. var divs= document.getElementsByClassName("cls");
    37. var d1=divs[0];
    38. d1.innerHTML="我真服辣666";
    39. script>
    40. body>
    41. html>

    2.案例

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <img src="img/off.gif" id="h1"><br><br>
    10. <div class="cls">传智教育div><br>
    11. <div class="cls">黑马程序员div><br>
    12. <input type="checkbox" name="hobby">电影
    13. <input type="checkbox" name="hobby">旅游
    14. <input type="checkbox" name="hobby">游戏
    15. <font color="red"> font>
    16. body>
    17. <script>
    18. //1.点亮灯泡:src 属性值
    19. var img =document.getElementById("h1");
    20. img.src="img/on.gif";
    21. //2.将所有div标签的内容加上:very good(红色字体)
    22. var divs=document.getElementsByTagName("div");
    23. for (let i = 0; i < divs.length; i++) {
    24. const div = divs[i];
    25. div.innerHTML+="very good ";
    26. }
    27. //3.使所有复选框呈现选中状态
    28. var hobbys =document.getElementsByName('hobby');
    29. for (let i = 0; i < hobbys.length; i++) {
    30. const hobby = hobbys[i];
    31. hobby.checked=true;
    32. }
    33. script>
    34. html>

    查阅:w3school 在线教程

    六、JS事件监听

    1.事件绑定

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <input type="button" id="btn1" value="事件绑定1" onclick="on()">
    10. <input type="button" id="btn2" value="事件绑定2">
    11. body>
    12. <script>
    13. function on() {
    14. alert("按钮1被点击了...");
    15. }
    16. document.getElementById('btn2').onclick=function(){
    17. alert("按钮2被点击了...");
    18. }
    19. script>
    20. html>

    2.常见的事件

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>JS-事件-常见事件title>
    8. head>
    9. <body onload="load()">
    10. <form action="" style="text-align: center;" onsubmit="subfn()">
    11. <input type="text" name="username" onblur="bfn()" onfocus="ffn()" onkeydown="kfn()">
    12. <input id="b1" type="submit" value="提交">
    13. <input id="b1" type="button" value="单击事件" onclick="fn1()">
    14. form>
    15. <br><br><br>
    16. <table width="800px" border="1" cellspacing="0" align="center" onmouseover="over()" onmouseout="out()">
    17. <tr>
    18. <th>学号th>
    19. <th>姓名th>
    20. <th>分数th>
    21. <th>评语th>
    22. tr>
    23. <tr align="center">
    24. <td>001td>
    25. <td>张三td>
    26. <td>90td>
    27. <td>很优秀td>
    28. tr>
    29. <tr align="center">
    30. <td>002td>
    31. <td>李四td>
    32. <td>92td>
    33. <td>优秀td>
    34. tr>
    35. table>
    36. body>
    37. <script>
    38. //onload : 页面/元素加载完成后触发
    39. function load(){
    40. console.log("页面加载完成...")
    41. }
    42. //onclick: 鼠标点击事件
    43. function fn1(){
    44. console.log("我被点击了...");
    45. }
    46. //onblur: 失去焦点事件
    47. function bfn(){
    48. console.log("失去焦点...");
    49. }
    50. //onfocus: 元素获得焦点
    51. function ffn(){
    52. console.log("获得焦点...");
    53. }
    54. //onkeydown: 某个键盘的键被按下
    55. function kfn(){
    56. console.log("键盘被按下了...");
    57. }
    58. //onmouseover: 鼠标移动到元素之上
    59. function over(){
    60. console.log("鼠标移入了...")
    61. }
    62. //onmouseout: 鼠标移出某元素
    63. function out(){
    64. console.log("鼠标移出了...")
    65. }
    66. //onsubmit: 提交表单事件
    67. function subfn(){
    68. alert("表单被提交了...");
    69. }
    70. script>
    71. html>

    3.案例

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>JS-事件-案例title>
    8. head>
    9. <body>
    10. <img id="light" src="img/off.gif"> <br>
    11. <input type="button" value="点亮" onclick="on()">
    12. <input type="button" value="熄灭" onclick="off()">
    13. <br> <br>
    14. <input type="text" id="name" value="ITCAST" onblur="upper()" onfocus="lower()" >
    15. <br> <br>
    16. <input type="checkbox" name="hobby"> 电影
    17. <input type="checkbox" name="hobby"> 旅游
    18. <input type="checkbox" name="hobby"> 游戏
    19. <br>
    20. <input type="button" value="全选" onclick="checkAll()">
    21. <input type="button" value="反选" onclick="reverse()" >
    22. body>
    23. <script>
    24. //1. 点击 "点亮" 按钮, 点亮灯泡; 点击 "熄灭" 按钮, 熄灭灯泡;
    25. function on(){
    26. var light= document.getElementById('light');
    27. light.src="img/on.gif";
    28. }
    29. function off(){
    30. var light= document.getElementById('light');
    31. light.src="img/off.gif";
    32. }
    33. //2. 输入框聚焦后, 展示小写; 输入框离焦后, 展示大写;
    34. function lower(){
    35. var name=document.getElementById('name');
    36. name.value=name.value.toLowerCase();
    37. }
    38. function upper(){
    39. var name=document.getElementById('name');
    40. name.value=name.value.toUpperCase();
    41. }
    42. //3. 点击 "全选" 按钮使所有的复选框呈现选中状态 ; 点击 "反选" 按钮使所有的复选框呈现取消勾选的状态 ; -- onclick
    43. function checkAll(){
    44. var hobbys=document.getElementsByName('hobby');
    45. for (let i = 0; i < hobbys.length; i++) {
    46. const element = hobbys[i];
    47. element.checked=true;
    48. }
    49. }
    50. function reverse(){
    51. var hobbys=document.getElementsByName('hobby');
    52. for (let i = 0; i < hobbys.length; i++) {
    53. const element = hobbys[i];
    54. element.checked=false;
    55. }
    56. }
    57. script>
    58. html>

  • 相关阅读:
    48、Nio(Io模型(异步(异步和同步)))
    WEB漏洞原理之---【组件安全】
    基于复合粒子群优化的模糊神经预测控制的研究(Matlab代码实现)
    基于C++11的数据库连接池(推荐)
    R3LIVE代码详解(三)
    【Linux学习笔记】调试工具gdb
    【Python】Pandas数据处理教程
    02 Infini-gateway部署实战+ES热备测试
    Docker安装Python3教程
    在 Elasticsearch 中实现自动完成功能 1:Prefix queries
  • 原文地址:https://blog.csdn.net/m0_64703222/article/details/133241817