JavaScript

[复制链接]
发表于 2026-1-26 07:35:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
js根本范例

1,在js中有根本范例(string,number,boolean,null,undefined,Symbol),和对象范例
 Function,Array,Date,Regexp.  
 对象范例中除了以上的,还可以使用Object来自界说对象范例。
2. 界说方式三种:
  - 第一种:使用{}
  - 第二种:使用Object关键字
  - 第三种:使用函数模拟构造器
  1. <!DOCTYPE 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>js的Object类型的应用</title>
  7. </head>
  8. <body>
  9. </body>
  10. <script>
  11.     var age;
  12.     function f1() {
  13.         console.log(1 + 2);
  14.     }
  15.     var emp = {
  16.         "name": "zhangsan",
  17.         age: 21,   // 属性名可以使用变量。
  18.         "gender": "男",
  19.         "sayHi": function () {
  20.             console.log(this.name + "说:我今年" + this.age + "岁");
  21.         },
  22.         "sum": f1
  23.     }
  24.     //访问对象的属性和方法
  25.     console.log(emp.name);
  26.     console.log(emp["age"]);
  27.     console.log(emp['age']);
  28.     console.log(emp.sayHi); //调用方法时,如果不带(), 其实就是调用属性,将函数当成值进行显示
  29.     console.log(emp.sayHi()); //调用方法时,应该带上(),才会执行函数的逻辑体
  30.     emp.sum()
  31.     //使用object定义
  32.     var student = new Object();
  33.     //使用变量来绑定对象的属性和方法
  34.     student.name = "michael"
  35.     student.age = 17
  36.     student.study = function () { console.log(this.name + "喜欢学习到深夜") }
  37.     student.sleep = function () { console.log(this.name + "喜欢在白天睡觉") }
  38.     //调用
  39.     console.log(student.name);
  40.     console.log(student.age);
  41.     console.log(student.study);
  42.     student.study();
  43.     //第三种方式模拟构造器
  44.     function Teacher(name, age, gender) {
  45.         this.name = name;
  46.         this.age = age;
  47.         this.gender = gender;
  48.         this.intro = function () {
  49.             console.log(this.name + "," + this.age + "," + this.gender)
  50.         };
  51.         this.getResult = function (a, b) {
  52.             return a + b;
  53.         }
  54.     }
  55.     var t1 = new Teacher("lucy", 15, 'f');
  56.     console.log(t1);
  57.     console.log(t1.name);
  58.     console.log(t1.getResult(2, 3));
  59.     console.log(t1.getResult);
  60. </script>
  61. </html> `
复制代码
json字符串JS对象相互转换
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9.     <button onclick="f1()">json字符串(js对象/js数组)转成JS对象</button>
  10.     <button onclick="f2()">JS对象转成json字符串(js对象/js数组)</button>
  11. </body>
  12. <script>
  13.     function f2() {
  14.         var age;
  15.         var teacher = { "name": "王大锤", age: 21, "friends": ["王老大", "张老二"], "sayHi": function () { console.log("你好"); } }
  16.         console.log(teacher.name);
  17.         console.log(teacher.friends[1]);
  18.         teacher.sayHi();
  19.         //将js对象teacher,转成JSON字符串, 调用内置的函数:JSON.stringify(),将js对象传入即可
  20.         var JSONStr = JSON.stringify(teacher);
  21.         console.log(typeof JSONStr);
  22.         console.log(JSONStr);
  23.         console.log(teacher);
  24.     }
  25.     function f1() {
  26.         //使用json格式的字符串
  27.         var student = '{"name":"michael","age":21,"hobby":["book","music"]}';
  28.         console.log(typeof student); //string
  29.         //获取字符串里的michael子串。
  30.         var str1 = student.substring(9, 16);
  31.         console.log(str1);
  32.         //将json字符串转成js对象:  JSON.parse()  ,只需要将json字符串传入到()里,即可
  33.         var jsObject = JSON.parse(student);
  34.         console.log(typeof jsObject);
  35.         //访问属性
  36.         console.log(jsObject.name);
  37.         console.log(jsObject.hobby[0]);
  38.         console.log(jsObject.hobby[1]);
  39.     }
  40. </script>
  41. </html>
复制代码
关系运算符
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. </body>
  10. <script>
  11.     var a = 10;
  12.     var b = 25;
  13.     var r1 = a + b;
  14.     var r2 = a - b;
  15.     var r3 = a * b;
  16.     var r4 = b / a;
  17.     var r5 = a % b;
  18.     var r6 = a++;
  19.     var r7 = --b;
  20.     console.log(r1, r2, r3, r4, r5, r6, r6);
  21.     //关系运算符: > >=, <,<=, ==,!=,===,!==
  22.     var x1 = 4;
  23.     var x2 = 5;
  24.     var re1 = x1 == x2;
  25.     console.log(re1);
  26.     var re2 = x1 != x2;
  27.     console.log(re2);
  28.     // 全等: 判断值与类型, 值和类型一样时,才会返回true
  29.     var m1 = "t";
  30.     var n1 = 2;
  31.     var re3 = m1 === n1;
  32.     // 不全等:  判断值与类型, 只要有一个不一样,就返回true.
  33.     var re4 = m1 !== n1;
  34.     console.log(re3, re4);
  35.     console.log(m1 == n1);
  36.     //支持三目运算符
  37.     var re5 = x2 < x1 ? "王大锤" : "张三";
  38.     console.log(re5);
  39.     //逻辑运算符
  40.     var a = 1;
  41.     var b = 2;
  42.     var c = 3;
  43.     var d = 4;
  44.     //先验证&&
  45.     var r1 = a > b && c++ < d;
  46.     console.log(r1, c);  //false 3
  47.     //再验证||
  48.     var r1 = a < b || c < d++;
  49.     console.log(r1, d);  // true  4
  50. </script>
  51. </html>
复制代码
window的对话框
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9.     <button onclick="testAlert()">测试alert框: 用于提示警告</button>
  10.     <button onclick="testConfirm()">测试确认框: 有确认和取消按钮的提示框</button>
  11.     <button onclick="testPrompt()">测试输入框</button>
  12. </body>
  13. <script>
  14.     function testAlert() {
  15.         window.alert("hello world"); //会阻塞后续的代码执行
  16.         alert("hello js");
  17.         var name = "高圆圆";
  18.         alert(name)
  19.         console.log("zhangsan");  //将内容打印到调试界面的控制台上
  20.     }
  21.     function testConfirm() {
  22.         var f = window.confirm("你确定要提交吗")
  23.         if (f) {
  24.             console.log("正在向服务器提交数据....");
  25.         } else {
  26.             console.log("不提交了....");
  27.         }
  28.     }
  29.     function testPrompt() {
  30.         //输入框:  第一个参数是提示文字,第二个参数是输入框里的默认值。
  31.         var username = prompt("请输入用户名", "zhangsan")
  32.         //输入框有确认和取消按钮, 确认表示提交输入框里的信息并返回,可能是具体值,也可能是
  33.         // 空字符串。  取消按钮提交的null
  34.         console.log(username)
  35.     }
  36. </script>
  37. </html>
复制代码
window的history和location
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9.     <button onclick="f1()">history</button>
  10.     <button onclick="f2()">location</button>
  11. </body>
  12. <script>
  13.     /*
  14.       location: window的一个属性对象,用来表示浏览器的地址信息
  15.      */
  16.     function f2() {
  17.         console.log(window.location);
  18.         //获取具体的url及其属性
  19.         console.log(location.href);
  20.         console.log(location.hostname);
  21.         console.log(location.pathname);
  22.         console.log(location.protocol);
  23.         //修改href的值,跳转到对应的网页地址
  24.         location.href = "http://www.baidu.com"
  25.     }
  26.     /*
  27.         history是window的一个属性对象。用来表示浏览器访问的历史记录
  28.     */
  29.     function f1() {
  30.         var his = window.history;
  31.         console.log(his);
  32.         //向前
  33.         //his.forward(); //相当于浏览器的向右箭头
  34.         //向后
  35.         //his.back();  //相当于浏览器的向左箭头
  36.         his.go(-1); //-1 表示后退, 1表示前进,0表示刷新
  37.     }
  38. </script>
  39. </html>
复制代码
window的screen和navigator
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9. </body>
  10. <script>
  11.     // 从window上获取screen屏幕对象
  12.     console.log(window.screen.availHeight);
  13.     console.log(window.screen.availWidth);
  14.     console.log(window.screen.height);
  15.     console.log(window.screen.width);
  16.     console.log(window.screen.colorDepth); //色彩深度
  17.     console.log(window.screen.pixelDepth); //色彩分辨率
  18.     //
  19.     console.log(navigator.userAgent);
  20. </script>
  21. </html>
复制代码
window的计时变乱方法
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9.     <button onclick="testSetTimeOut()">一次性时钟:让代码延迟指定的毫秒数后再执行</button>
  10.     <button onclick="testClearTimeOut()">在执行前,取消一次性时钟</button>
  11.     <button onclick="testSetInterval()">周期性时钟:指定间隔的毫秒数重复的执行</button>
  12.     <button onclick="testClearInterval()">取消周期性时钟</button>
  13.     <p id="p1"></p>
  14. </body>
  15. <script>
  16.     var l;
  17.     function testSetTimeOut() {
  18.         /*  setTimeout(timeHandler,delay)
  19.                 第一个参数:timeHandler,  要执行的代码逻辑,可以是一个函数。
  20.                 第二个参数:delay,  指定的延迟时间,单位是毫秒。
  21.          */
  22.         l = window.setTimeout(function () {
  23.             console.log("hello world")
  24.         }, 3000)
  25.     }
  26.     function testClearTimeOut() {
  27.         /* 一次性时钟,是可以在执行前进行取消的操作的
  28.                 */
  29.         window.clearTimeout(l)
  30.     }
  31.     var p =document.getElementById("p1");
  32.     var intervalTime;
  33.     function testSetInterval() {
  34.         /*
  35.             setInterval(timeHandler,period)
  36.             第一个参数:timeHandler,  要重复执行的代码逻辑,可以是一个函数。
  37.             第二个参数:间隔时间,单位是毫秒
  38.         */
  39.         intervalTime = setInterval(function () {
  40.             var d = new Date();
  41.             var hour = d.getHours();
  42.             var minute = d.getMinutes();
  43.             var second = d.getSeconds();
  44.             var time = hour + ":" + minute + ":" + second;
  45.             p.innerText = time;
  46.         }, 1000)
  47.     }
  48.     function testClearInterval() {
  49.         window.clearInterval(intervalTime);
  50.     }
  51. </script>
  52. </html>
复制代码
document

1. js将整个HTML文档封装到了一个window的属性document里。在document内里维护了一个树形的条理布局。在这个布局中生存了HTML的全部元素及其元素的属性和文本。
2. 可以使用document来查找想要的节点
 - 通过id  :    getElementById( id值 )
 - 通过标署名:    getElementsByTagName( 标署名)
 - 通过class名:  getElementsByClassName( 类名)
 - 通过条理布局:  
 -- parentNode/parentElement:  返回的是当前节点的父节点
 -- childNodes:  获取的节点个数以及节点,与换行有关系。
 -- children:  会返回真正的子元素节点。
  -- firstChild/firstElementChild
  -- lastChild/lastElementChild
  -- previousSibling/previousElementSibling
  -- nextSibling/nextElementSibling
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9.     <div id="d1">
  10.         <div id="d2">
  11.             <p class="c1">我是一个段落1</p>
  12.         </div>
  13.         <div id="d3">
  14.             <input id="in1" class="c1" type="text" value="zhangsan">
  15.             <p>我是一个段落2</p>
  16.         </div>
  17.         <div id="d4">
  18.         </div>
  19.     </div>
  20. </body>
  21. <script>
  22.    
  23.     // js里返回的都是对象
  24.     var input = document.getElementById("in1");
  25.     console.log(input); //  整个元素信息,包括属性,文本,子元素
  26.     console.log(input.value);
  27.     input.value = "lisi";
  28.     //通过标签名查找
  29.     var ps = document.getElementsByTagName("p");
  30.     console.log("长度:" + ps.length);
  31.     console.log(ps[0].innerText);
  32.     console.log(ps[1].innerText);
  33.     ps[0].innerText = "我是michael"
  34.     console.log(ps[0].nodeName);
  35.     console.log(ps[0].nodeType);
  36.     console.log(ps[0].nodeValue);
  37.     console.log(ps[0].innerHTML);
  38.     var d3 = document.getElementById("d3");
  39.     console.log(d3.innerText);
  40.     console.log(d3.innerHTML);
  41.     //通过类名获取节点
  42.     var cs = document.getElementsByClassName("c1");
  43.     console.log(cs.length);
  44.     console.log(cs[0].nodeName);
  45.     console.log(cs[1].nodeName);
  46.     //通过层次关系查找元素:
  47.     var input1 = document.getElementById("in1");
  48.     var parent = input1.parentElement
  49.     console.log(parent.nodeName);
  50.     //parent.innerHTML = "<button>按钮</button>"
  51.     //获取parent这个节点对象的所有孩子
  52.     var cs = parent.childNodes
  53.     console.log(cs.length);
  54.     console.log(cs);
  55.     var cs2 = parent.children;
  56.     console.log(cs2.length);
  57.     console.log(cs2);
  58.     //var c3 = parent.firstChild
  59.     var c3 = parent.firstElementChild
  60.     console.log(c3.nodeName);
  61.     console.log(c3);
  62.     //var c4 = parent.lastChild
  63.     var c4 = parent.lastElementChild
  64.     console.log(c4.nodeName);
  65.     console.log(c4);
  66.     console.log("------------------------------")
  67.     //查找兄弟
  68.     var div3 = document.getElementById("d3");
  69.     //var brother1 = div3.previousSibling
  70.     var brother1 = div3.previousElementSibling
  71.     console.log(brother1);
  72.     console.log(brother1.nodeName);
  73.     //var brother2 = div3.nextSibling
  74.     var brother2 = div3.nextElementSibling
  75.     console.log(brother2);
  76.     console.log(brother2.nodeName);
  77. </script>
  78. </html>
复制代码
DOM的创建节点
  1. <!DOCTYPE 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>Document</title>
  7.     <style>
  8.         #d1 {
  9.             border: 1px solid red;
  10.             width: 500px;
  11.             height: 500px;
  12.         }
  13.         #d2 {
  14.             width: 200px;
  15.             height: 200px;
  16.             background-color: aqua;
  17.         }
  18.         #d3 {
  19.             width: 200px;
  20.             height: 200px;
  21.             background-color: pink;
  22.         }
  23.     </style>
  24.     <script>
  25.         /* window的onload事件 可以绑定一个匿名函数。在函数里面写代码逻辑。
  26.            执行时机:  当整个HTML被浏览器加载完毕后,才会执行该onload事件。
  27.         */
  28.         window.onload = function () {
  29.             var p = document.createElement("p");
  30.             p.setAttribute("id", "p1")
  31.             p.setAttribute("class", "c1")
  32.             var text = document.createTextNode("我是一个段落");
  33.             p.appendChild(text);
  34.             console.log(p);
  35.             //定位d2元素
  36.             var d2 = document.getElementById("d2");
  37.             // appendChild(newNode):  添加到父节点中的最后一个子节点后。
  38.             d2.appendChild(p)
  39.             var p1 = document.createElement("p");
  40.             p1.setAttribute("name", "myP")
  41.             var txt = document.createTextNode("锄禾日当午")
  42.             p1.appendChild(txt)
  43.             //将p1插入到d3前面。  // insertBefore(newNode,reNode):  
  44.             var d1 = document.getElementById("d1")
  45.             var d3 = document.getElementById("d3");
  46.             d1.insertBefore(p1, d3);
  47.         }
  48.     </script>
  49. </head>
  50. <body>
  51.     <div id="d1">
  52.         <div id="d2"></div>
  53.         <div id="d3"></div>
  54.     </div>
  55. </body>
  56. </html>
复制代码
DOM的更换和删除
  1. <!DOCTYPE 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>Document</title>
  7.     <script>
  8.         window.onload = function () {
  9.             //创建一个新的span节点
  10.             var span = document.createElement("span");
  11.             var txt = document.createTextNode("用户名已经被占用")
  12.             span.appendChild(txt);
  13.             //获取父节点
  14.             var d2 = document.getElementById("d2");
  15.             var p1 = document.getElementById("p1")
  16.             //进行替换操作
  17.             d2.replaceChild(span, p1)
  18.             //移除d2这个节点
  19.             var d1 = document.getElementById("d1")
  20.             d1.removeChild(d2)
  21.             //获取d1里的属性值
  22.             var name = d1.getAttribute("name");
  23.             console.log(name);
  24.             //移除d1里的class属性
  25.             d1.removeAttribute("class")
  26.         }
  27.     </script>
  28. </head>
  29. <body>
  30.     <div id="d1" class="c1" name="n1">
  31.         <div id="d2">
  32.             <p id="p1">我是一个段落</p>
  33.         </div>
  34.     </div>
  35.     <form action="">
  36.     </form>
  37.     <form action=""></form>
  38. </body>
  39. </html>
复制代码
变乱模子
  1. <!DOCTYPE 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>Document</title>
  7.     <script>
  8.         window.onload = function () {
  9.             var bn2 = document.getElementById("bn2");
  10.             //使用DOM0级方式绑定事件
  11.             bn2.onclick = function () {
  12.                 alert(Math.pow(2, 3));
  13.             }
  14.             //如果DOM0事件,连续绑定相同的事件,会进行覆盖
  15.             bn2.onclick = function () {
  16.                 alert(Math.pow(3, 3));
  17.             }
  18.             //使用DOM2级方式绑定事件
  19.             var bn3 = document.getElementById("bn3");
  20.             bn3.addEventListener('click', function () {
  21.                 alert("DOM2级绑定事件使用addEventListener方法")
  22.             })
  23.             //DOM2级方式,连续绑定相同的事件,不会出现覆盖情况
  24.             function f2() {
  25.                 alert("hello world")
  26.             }
  27.             //绑定事件时,如果设置的是有名字的函数,不要带括号。
  28.             bn3.addEventListener('click', f2)
  29.             //取消DOM2级事件,  当初绑定的事件处理函数,必须有名字,才能取消。
  30.             bn3.removeEventListener("click", f2)
  31.         }
  32.     </script>
  33. </head>
  34. <body>
  35.     <button onclick="alert('我是按钮')">DOM0级:HTML里添加事件</button>
  36.     <button id="bn2">DOM0级:在script标签里添加事件</button>
  37.     <button id="bn3">DOM2级:在script标签里添加事件</button>
  38. </body>
  39. </html>
复制代码
event对象的应用
  1. <!DOCTYPE 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>Document</title>
  7. </head>
  8. <body>
  9.     <div onclick="f1(1,event)">我是一个div</div>
  10.     <div onclick="f2()">
  11.         <p onclick="f3(event)">
  12.             <a href="http://www.baidu.com" onclick="f4(event)">我是一个链接</a>
  13.         </p>
  14.     </div>
  15. </body>
  16. <script>
  17.     /* event:  该对象是在事件触发式,自动产生的
  18.        如何使用该对象:  将event这个值,赋值给形参即可。 形参名可以是任何名字
  19.     */
  20.     function f1(a, e) {
  21.         // target: 事件源那个元素对象
  22.         console.log(e.target.nodeName);
  23.         console.log(e.currentTarget.nodeName);
  24.         //事件的名称
  25.         console.log(e.type)
  26.         console.log(e.clientX, e.clientY);
  27.         console.log(e.cancelable);
  28.     }
  29.     function f2() {
  30.         console.log("div");
  31.     }
  32.     function f3(event) {
  33.         console.log("p");
  34.         event.cancelBubble = true;
  35.     }
  36.     function f4(event) {
  37.         console.log("a");
  38.         event.preventDefault();
  39.         event.stopPropagation();
  40.     }
  41. </script>
  42. </html>
复制代码







免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表