JavaScript
<h1 id="1什么是javascript">1、什么是JavaScript</h1><h2 id="11概述">1.1、概述</h2>
<p>JavaScript是一门世界上最流行的脚本语言</p>
<p>Java、JavaScript</p>
<p><strong>一个合格的后端程序员,必须精通JavaScript</strong></p>
<h2 id="12历史">1.2、历史</h2>
<p><strong>https://www.cnblogs.com/ghost-xyx/p/4035615.html</strong></p>
<p><strong>ECMAScript</strong> 可以理解为是JavaScript的一个标准</p>
<p>最新版本已经到es6版本~</p>
<p>但是大部分浏览器只停留在支持es5代码上!</p>
<p>开发环境----上线环境,版本不一致</p>
<hr>
<h1 id="2快速入门">2、快速入门</h1>
<h2 id="21引入javascript">2.1、引入JavaScript</h2>
<p>1、内部标签</p>
<pre><code class="language-javascript"><script>
//......
</script>
</code></pre>
<p>2、外部标签</p>
<p>abs.js</p>
<pre><code class="language-javascript">//...
</code></pre>
<p>test.html</p>
<pre><code class="language-javascript"><script src="js/yr.js"></script>
</code></pre>
<p>测试代码</p>
<pre><code class="language-javascript"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--scrpit标签哪,写JavaScript代码-->
<!--<script>
alert('hello,world');
</script>-->
<!--外部引入-->
<!--注意:script千万不能使用自闭合标签,必须成对出现-->
<script src="js/yr.js"></script>
</head>
<body>
<!--这里也可以存放js代码-->
</body>
</html>
</code></pre>
<h2 id="22基本语法入门">2.2、基本语法入门</h2>
<pre><code class="language-javascript"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--JavaScript严格区分大小写-->
<script>
//1.定义变量
var score = 71;
// alert(num);
//2.条件控制
if (score>60 && score<70){
alert("60~70");
}else if (score>70 && score<80){
alert("70~80");
}else{
alert("other");
}
//console.log(score)在浏览器的控制台打印输出变量!sout
</script>
</head>
<body>
</body>
</html>
</code></pre>
<h2 id="23数据类型">2.3、数据类型</h2>
<p>数值,文本,图形,音频,视频......</p>
<p><strong>变量</strong></p>
<pre><code class="language-javascript">var a = 1;
</code></pre>
<p><strong>number</strong></p>
<p>js不区分小数和整数,Number</p>
<pre><code class="language-javascript">123//整数123
123.1//浮点数123.1
1.23e3//科学计数法
-99//负数
NaN//not a number(不是一个数字)
Infinity//∞无限大∞
</code></pre>
<p><strong>字符串</strong></p>
<p>'abc'"abc"</p>
<p><strong>布尔值</strong><br>
true,false</p>
<p><strong>逻辑运算</strong></p>
<pre><code>&& 两个都为真,结果为真
||一个为真,结果为真
! 真即假,假即真(取反)
</code></pre>
<p><strong>比较运算符</strong></p>
<pre><code>=
== 等于(类型不一样,值一样,也会判断为true)
=== 绝对等于(类型一样,值一样,结果为true)
</code></pre>
<p>这是一个JS的缺陷,坚持不要使用==比较</p>
<p>须知:</p>
<ul>
<li>NaN===NaN,这个与所有的数值都不想等,包括自己</li>
<li>只能通过isNaN(NaN)来判断是不是NaN</li>
</ul>
<p><strong>浮点数问题</strong></p>
<pre><code class="language-javascript">console.log(1/3)===console.log(1-2/3);
</code></pre>
<p>尽量不要使用浮点数进行运算,存在精度问题</p>
<p>解决办法</p>
<pre><code class="language-javascript">console.log(Math.abs(1/3-(1-2/3))<0.0000000000001)
</code></pre>
<p>null和undefined</p>
<ul>
<li>null 空</li>
<li>undefined 未定义</li>
</ul>
<p><strong>数组</strong><br>
Java的数组必须是相同类型的对象~,JS中则不需要这样!</p>
<pre><code class="language-javascript">//保证代码的可读性,尽量使用第一种方式来定义
var arr =
new Array(1,2,3,4,5,'hello',null,true)
</code></pre>
<p>取数组下标:如果越界了,就会</p>
<pre><code class="language-javascript">undefined
</code></pre>
<p><strong>对象</strong></p>
<p>对象是大括号,数组是中括号~~</p>
<p>每个属性之间使用逗号隔开,最后一个不需要添加</p>
<pre><code class="language-javascript"> //Person person = new Person(1,2,3,4,5);
var person={
name:"yinrui",
age:3,
tags:['js','java','web','...']
}
</code></pre>
<p>取对象的值</p>
<pre><code class="language-javascript">person.name
person.age
</code></pre>
<h2 id="24严格检查格式">2.4、严格检查格式</h2>
<pre><code class="language-javascript"><!--
前提IDEA需要支持ES6语法
'use strict';严格检查模式,预防JavaScript的随意性导致产生的一些问题
必须写在JavaScript的第一行
局部变量建议都使用 let去定义
-->
<script>
'use strict'
//全局变量
i = 1;
//ES6 let
</script>
</code></pre>
<hr>
<h1 id="3数据类型">3、数据类型</h1>
<h2 id="31字符串">3.1、字符串</h2>
<p>1、正常字符串我们使用 单引号,或者双引号包裹</p>
<p>2、注意转移字符</p>
<pre><code class="language-javascript">\'
\n
\t
\u4e2d \u#### Unicode字符
\x41 Ascii字符
</code></pre>
<p>3、多行字符串编写</p>
<pre><code class="language-javascript"> //` 在tab键上面包裹可以使用多行字符串
var msg = `hello
world
你好呀
你还`
</code></pre>
<p>4、模版字符串</p>
<pre><code class="language-javascript">let name = "yinrui";
let msg = `你好呀,${name}`;
</code></pre>
<p>5、字符串长度</p>
<pre><code class="language-javascript">student.length
</code></pre>
<p>6、字符串的可变性,不可变</p>
<p>7、大小写转换</p>
<pre><code class="language-javascript">//注意,这里是方法,不是属性了
student.toUpperCase()
student.toLowerCase()
</code></pre>
<p>8、student.indexOf('t');</p>
<p>9、subString</p>
<pre><code class="language-java">[)
student.subString(1)//从第一个字符串截取到最后一个字符串
student.subString(1,3)//[1,3)
</code></pre>
<h2 id="32数组">3.2、数组</h2>
<p><strong>Array可以包含任意的数据类型</strong></p>
<pre><code class="language-javascript">var arr = //通过下标取值和赋值
arr
arr = 1
</code></pre>
<p>1、长度</p>
<pre><code class="language-javascript">arr.length
</code></pre>
<p><strong>注意:假如给arr.length赋值,数组大小就会发生变化~如果赋值过小,元素就会丢失</strong></p>
<p>2、indexOf,通过元素获得下标索引</p>
<pre><code class="language-javascript">arr.indexOf(2)
1
</code></pre>
<p>字符串的“1”和数字1是不同的</p>
<p>3、slic()截取数组Array的一部分,返回一个新的数组,类似于String中的subString</p>
<p>4、push,pop</p>
<pre><code class="language-javascript">push():压入到尾部
pop:弹出尾部的一个元素
</code></pre>
<p>5、unshift(),shift() 头部</p>
<pre><code class="language-javascript">unshift():压入到头部
shift():弹出头部的一个元素
</code></pre>
<p>6、排序sort()</p>
<pre><code class="language-javascript">(8)
arr.sort()
(8)
</code></pre>
<p>7、元素反转revers()</p>
<pre><code class="language-javascript">(8)
arr.reverse()
(8)
</code></pre>
<p>8、concat()</p>
<pre><code class="language-javascript">(8)
arr.concat()
(11)
</code></pre>
<p>//注意⚠️concat()并没有修改数组,只是会返回一个新的数组</p>
<p>9、连接符 join</p>
<p>打印拼接数组,使用特定的字符串连接🔗</p>
<pre><code class="language-javascript">(3) ["a", "b", "c"]
arr.join('-')
"a-b-c"
</code></pre>
<p>10、多维数组</p>
<pre><code class="language-javascript">var arr = [,,["5","6"]]
arr
4
</code></pre>
<p>数组:存储数据(如何存,如何取,方法都可以自己实现!)</p>
<h2 id="33对象">3.3、对象</h2>
<p>若干个键值对</p>
<pre><code class="language-javascript">var 对象名 = {
属性名:属性值,
属性名:属性值,
属性名:属性值
}
//定义了一个person对象,他有4个属性
const person = {
name: "yinrui",
age: 20,
email: "1455395994@qq.com",
score: 99
}
</code></pre>
<p>js对象中,{....}表示一个对象,键值对描述属性:xxxx:xxxx,多个属性之间使用逗号隔开,最后一个属性不加逗号!</p>
<p>JavaScript中所有的键都是字符串,值都是任意的对象!</p>
<p>1、对象赋值</p>
<pre><code class="language-javascript">person.name = "yinrui"
"yinrui"
person.name
yinrui
</code></pre>
<p>2、使用一个不存在的对象属性,不会报错!undefined</p>
<p>person.haha</p>
<p>undefind</p>
<p>3、动态的删除属性,同伙delete删除对象的属性</p>
<pre><code class="language-javascript">delete person.name
true
person
</code></pre>
<p>4、动态的添加属性,直接给新的属性添加值即可</p>
<pre><code class="language-javascript">person.haha = "haha"
"haha"
person
</code></pre>
<p>5、判断属性值是否在属性中! xxx in xxx!</p>
<pre><code class="language-javascript">'age' in person
true
//继承
'toString' in person
true
</code></pre>
<p>6、判断一个属性是否是这个对象自身拥有的hasOwnProperty()</p>
<pre><code class="language-javascript">person.hasOwnProperty('age');
true
</code></pre>
<h2 id="34流程控制">3.4、流程控制</h2>
<p>if判断</p>
<pre><code class="language-javascript"> var age = 3;
if(age>3){
alert('haha');
}else if(age<5){
alert("kuku");
}else{
alert('kuwa~');
}
</code></pre>
<p>while循环,避免程序死循环</p>
<pre><code class="language-javascript"> while(age<100){
age = age+1;
console.log(age);
}
do{
age = age+1
console.log(age)
}while(age<100)
</code></pre>
<p>for循环♻️</p>
<pre><code class="language-javascript"> for (let i = 0; i < 100; i++) {
console.log(i)
}
</code></pre>
<p>for each循环♻️</p>
<pre><code class="language-javascript"> //函数
var age =
age.forEach(function (value) {
console.log(value)
})
</code></pre>
<p>For...in</p>
<pre><code class="language-javascript"> var age=
/*
* for(Type str:el){}
* */
//for(var index in object){}
for (var num in age){
console.log(age);
}
</code></pre>
<h2 id="35map和set">3.5、Map和Set</h2>
<blockquote>
<p>ES6的新特性~</p>
</blockquote>
<p>Map:</p>
<pre><code class="language-javascript"> 'use strict'
//ES6 Map
//学生的成绩,学生的名字
// var names = ["tom","jack","haha"];
// var scores = ;
const map = new Map([['tom', 100], ['jack', 90], ['haha', 80]]);
let name = map.get('tom');//通过ket获得value
map.set('admin',123345);//添加
console.log(name)
map.delete('tom');//删除
</code></pre>
<p>Set:无序不重复的集合</p>
<pre><code class="language-javascript"> const set = new Set();//set可以去重
set.add(4);//添加
set.delete(1);//删除
console.log(set.has(2));//是否包含某个元素!
</code></pre>
<h2 id="36iterator">3.6、iterator</h2>
<blockquote>
<p>es6新特性,for(var x of array)</p>
</blockquote>
<pre><code class="language-javascript"> const arr = ;
for(var x of arr){
console.log(x);
}
</code></pre>
<p>遍历Map数组</p>
<pre><code class="language-javascript"> let map = new Map([["tom",100],["jack",90],["haha",80]]);
for (var y of map)
{
console.log(y);
}
</code></pre>
<p>遍历Set集合</p>
<pre><code class="language-javascript"> let set = new Set();
for (var z of set){
console.log(z);
}
</code></pre>
<hr>
<h1 id="4函数及面向对象">4、函数及面向对象</h1>
<h2 id="41函数">4.1、函数</h2>
<blockquote>
<p>定义方式一</p>
</blockquote>
<p>绝对值函数</p>
<pre><code class="language-javascript">function abs(x){
if(x>=0){
return x;
}else{
return -x;
}
}
</code></pre>
<p>一旦执行到return代表函数结束,返回结果!</p>
<p>如果没有自信return,函数执行玩也会返回结果,结果就是undefined</p>
<blockquote>
<p>定义方式二</p>
</blockquote>
<pre><code class="language-javascript">var abs = function (x){
if(x>=0){
return x;
}else{
return -x;
}
}
</code></pre>
<p>function(x){......}这是一个匿名函数,但是可以把结果赋值给abs,通过abs就可以调用函数!</p>
<p>方式一和方式二等价!</p>
<blockquote>
<p>调用函数</p>
</blockquote>
<pre><code class="language-javascript">abs(10) //10
abs(-10) //10
</code></pre>
<p>参数问题:JavaScript可以传任意个参数,也可以不传参数~</p>
<p>参数进来是否存在的问题?</p>
<p>假设参数不存在,如何规避的问题</p>
<pre><code class="language-javascript"> function abs(x){
//手动抛出异常来判断
if (typeof x!== 'number'){
throw 'Not a Number';
}
if(x>=0){
return x;
}else{
return -x;
}
}
</code></pre>
<blockquote>
<p>arguments</p>
</blockquote>
<p>arguments是JS免费赠送的一个关键字</p>
<p>代表传递进来的所有的参数,是一个数组</p>
<pre><code class="language-javascript">var abs = function (x) {
console.log("x=>"+x);
for (var i=0;i<arguments.length;i++){
console.log(arguments);
}
if(x>=0){
return x;
}else{
return -x;
}
}
</code></pre>
<p>问题:arguments包含所有的参数,我们有时候想使用多余的参数来进行附加操作.需要排除已有的参数~</p>
<blockquote>
<p>rest</p>
</blockquote>
<p>以前:</p>
<pre><code class="language-javascript">function aaa(a,b){
console.log("a=>"+a);
console.log("b=>"+b);
if(arguments.length>2){
for(var i=2;i<arguments.length;i++){
//...
}
}
}
</code></pre>
<p>ES6引入的新特性,获取除了已经定义的参数之外的所有参数...</p>
<p>现在:</p>
<pre><code class="language-javascript">function aaa(a,b,...rest){
console.log("a=>"+a);
console.log("b=>"+b);
console.log(rest);
}
</code></pre>
<p>rest参数只能写在最后面,必须使用...标识</p>
<h2 id="42变量作用域">4.2、变量作用域</h2>
<p>在JavaScript中,var定义变量实际是有作用域的.</p>
<p>假设在函数体中声明,则在函数体外不可以使用~(非要想实现的话,后面可以研究一下<strong>闭包</strong>)</p>
<pre><code class="language-javascript">'use strict'
function yr() {
var x = 1;
x = x+1;
}
x = x+2; //Uncaught ReferenceError: x is not defined
</code></pre>
<p>如果两个函数使用了相同的变量名,只要在函数内部使用就不冲突</p>
<p>函数内部可以访问外部函数的成员,反之则不行🙅♂️</p>
<p>假设,内部函数变量和外部函数变量,重名!</p>
<p>假设在JavaScript中函数查找变量从自身函数开始~,由“内”向“外”查找,假设外部存在这个同名的函数变量,则内部函数会屏蔽玩不函数的变量.</p>
<blockquote>
<p>提升变量的作用域</p>
</blockquote>
<pre><code class="language-javascript">function yr(){
var x = "x" + y;
console.log(x);
var y = x + y;
}
</code></pre>
<p>结果:xundefined</p>
<p>说明:js执行引擎,自动提升了y的声明,但是不会提升变量y的赋值;</p>
<pre><code class="language-javascript">fuction yr2(){
//var a,b,c,d.......; 在最前面声明所有的变量
}
</code></pre>
<p>这个是在JavaScript建立之初就存在的特性.养成规范:所有的变量定义都放在函数的头部,不要乱放,便于代码的维护;</p>
<pre><code class="language-javascript">function yr2(){
var x = 1,
y = x + 1,
z,i,a; //undefined
//之后随意使用
a = x + 2;
}
</code></pre>
<blockquote>
<p>全局函数</p>
</blockquote>
<pre><code class="language-javascript"> 'use strict'
//全局变量
const x = 1;
function f(){
console.log(x);
}
f()
console.log(x);
</code></pre>
<p>全局对象window</p>
<pre><code class="language-javascript">var x = 'xxx';
alert(x);
alert(window.x);//默认所有的全局变量,都会自动绑定在window对象下
</code></pre>
<p>alert这个函数本身也是window对象的一个变量</p>
<p>JavaScript实际上只有一个全局作用域.任何变量(函数也可以视为变量),假设没有在函数左右范围内找到,就会向外查找,如果在全局作用域都没有找到,报错 <strong>RefrenceError</strong>引用异常</p>
<blockquote>
<p>规范</p>
</blockquote>
<p>由于我们所有的全局变量都会绑定到我们的window上,如果不同的js文件使用了相同的全局变量,冲突~ 如何能够减少冲突?</p>
<pre><code class="language-javascript">var yinruiApp = {};
yinruiApp.name = "yinrui";
yinruiApp.add = function (a,b) {
return a + b;
}
</code></pre>
<p>把自己的代码全部放入自己定义的唯一空间中,降低全局命名冲突的为题~</p>
<p>jQuery</p>
<blockquote>
<p>局部作用域</p>
</blockquote>
<pre><code class="language-javascript">function aaa() {
for (var i = 0; i < 100; i++) {
console.log(i);
}
console.log(i+1);//问题?i出了这个作用域还能够使用
}
</code></pre>
<p>ES6 let关键字,解决局部作用域冲突问题!</p>
<pre><code class="language-javascript">function aaa() {
for (let i = 0; i < 100; i++) {
console.log(i);
}
console.log(i+1);//Uncaught ReferenceError: i is not defined
}
</code></pre>
<p>建议大家都是用 <strong>let</strong> 去定义局部作用域的变量</p>
<blockquote>
<p>常量</p>
</blockquote>
<p>在ES6之前,怎么定义常量:只有用全部大写字母命名的变量就是常量,建议不要修改这样的值</p>
<pre><code class="language-javascript">var PI = '3.14';
console.log(PI)
PI = '123414';//可以改变这个值
console.log(PI)
</code></pre>
<p>在ES6引入了常量关键字const</p>
<pre><code class="language-javascript">const PI = '3.14';//只读变量
console.log(PI);
PI = '12412';//Attempt to assign to const or readonly variable
</code></pre>
<h2 id="43方法">4.3、方法</h2>
<blockquote>
<p>定义方法</p>
</blockquote>
<p>方法就是把函数放在对象的里面,对象只有两个东西:属性和方法</p>
<pre><code class="language-javascript"> name:'尹锐',
birth:2020,
//方法
age: function () {
//今年 - 出生的年份
let now = new Date().getFullYear();
return now - this.birth;
}
}
//属性
yinrui.name
//方法,方法一定要带()
yinrui.age()
</code></pre>
<p>this代表什么?拆开上面的代码看看</p>
<pre><code class="language-javascript"> function getAge() {
//今年 - 出生的年份
let now = new Date().getFullYear();
return now - this.birth;
}
var yinrui = {
name:'尹锐',
birth:2020,
//方法
age:getAge,
}
//yinrui.age() 👌
//window.getAge() 🙅
</code></pre>
<p>this是无法指向的名,是默认指向调用它的那个对象</p>
<blockquote>
<p>apply</p>
</blockquote>
<p>在js中可以控制this的指向</p>
<pre><code class="language-javascript">function getAge() {
//今年 - 出生的年份
let now = new Date().getFullYear();
return now - this.birth;
}
var yinrui = {
name:'尹锐',
birth:1999,
//方法
age:getAge,
}
getAge.apply(yinrui,[]);//this指向了yinrui这个对象,然后参数为空
</code></pre>
<hr>
<h1 id="5常用对象">5、常用对象</h1>
<blockquote>
<p>标准对象</p>
</blockquote>
<pre><code class="language-javascript">typeof 123
"number"
typeof '123'
"string"
typeof true
"boolean"
typeof NaN
"number"
typeof []
"object"
typeof {}
"object"
typeof Math.abs
"function"
typeof undefined
"undefined"
</code></pre>
<h2 id="51date">5.1、Date</h2>
<blockquote>
<p>基本使用</p>
</blockquote>
<pre><code class="language-javascript">let now = new Date();
console.log(now)
now.getFullYear();//年
now.getMonth();//月 从零开始计算!
now.getDate();//日
now.getDay();//星期
now.getHours();//时
now.getMinutes();//分
now.getSeconds();//秒
now.getTime();//时间戳 全世界统一 1970/01/01 00:00 到现在的毫秒数
console.log(new Date(1585905627082));//传入一个时间戳获取时间
</code></pre>
<p>转换</p>
<pre><code class="language-javascript">console.log(new Date(1585905627082));//传入一个时间戳获取时间
Fri Apr 03 2020 17:20:27 GMT+0800 (中国标准时间)
now.toLocaleString();
"2020/4/3 下午5:26:17"
now.toGMTString()
"Fri, 03 Apr 2020 09:26:17 GMT"
</code></pre>
<h2 id="52json">5.2、Json</h2>
<blockquote>
<p>Json是什么</p>
</blockquote>
<p>早期,所有的数据传输习惯使用xml文件!</p>
<ul>
<li>JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。</li>
<li>简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。</li>
<li>易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。</li>
</ul>
<p>在JavaScript一切皆对象,任何js支持的类型都可以用JSON表示</p>
<p>格式:</p>
<ul>
<li>对象都用{}</li>
<li>数组都用[]</li>
<li>所有的键值对都使用key:value</li>
</ul>
<p>JSON字符串和JS对象的转换</p>
<pre><code class="language-javascript"> var user = {
name:'yinrui',
age:3,
sex:'男'
}
//对象转换为json字符串 {"name":"yinrui","age":3,"sex":"男"}
var jsonUser = JSON.stringify(user)
//json字符串转换为对象 参数为json字符串
let obj = JSON.parse('{"name":"yinrui","age":3,"sex":"男"}');
</code></pre>
<p>很多人搞不清楚,JSON和JS对象的区别</p>
<pre><code class="language-javascript">var obj = {a:'hello',b:'hellob'}
var json = "{'a':'hello','b':'hellob'}"
</code></pre>
<hr>
<h1 id="6面向对象编程">6、面向对象编程</h1>
<blockquote>
<p>原型对象</p>
</blockquote>
<h2 id="61什么是面向对象">6.1、什么是面向对象</h2>
<p>类:模版</p>
<p>对象:具体的实例</p>
<p>在JavaScript中,需要大家换一下思维方式!</p>
<p>原型:</p>
<pre><code class="language-javascript">var Student = {
name:"yinrui",
age:3,
run:function () {
console.log(this.name+" run...");
}
};
var xiaoming = {
name:'xiaoming',
};
xiaoming.__proto__ = Student;
</code></pre>
<blockquote>
<p>class继承</p>
</blockquote>
<p>class关键字,是在ES6引入的</p>
<p>1、定义一个类,属性,方法</p>
<pre><code class="language-javascript"> //ES6之后===============
//定义一个学生的类
class Student{
constructor(name) {
this.name = name;
}
hello(){
alert('hello');
}
}
var xiaoming = new Student('xiaoming');
var xiaohong = new Student('xiaohong');
xiaoming.hello()
</code></pre>
<p>2、继承</p>
<pre><code class="language-javascript"> //ES6之后===============
//定义一个学生的类
class Student{
constructor(name) {
this.name = name;
}
hello(){
alert('hello');
}
}
var xiaoming = new Student('xiaoming');
class XiaoStudent extends Student{
constructor(name,grade) {
super(name);
this.grade = grade;
}
myGrade(){
alert('我是一名小学生');
}
}
var xiaohong = new XiaoStudent('xiaohong');
</code></pre>
<blockquote>
<p>原型链</p>
</blockquote>
<p>__proto__:所有对象都有这个属性,这个属性又指向了object本身,形成了一个原型链</p>
<hr>
<h1 id="7操作bom元素重点">7、操作Bom元素(重点)</h1>
<blockquote>
<h2 id="浏览器介绍">浏览器介绍</h2>
</blockquote>
<p>JavaScript和浏览器关系</p>
<p>JavaScript诞生就是问了能够让他在浏览器中运行</p>
<p>BOM:浏览器对象模型</p>
<ul>
<li>IE 6~11</li>
<li>Chrome</li>
<li>Safari</li>
<li>FireFox</li>
<li>Opera</li>
</ul>
<p><strong>三方</strong></p>
<ul>
<li>QQ浏览器</li>
<li>360浏览器</li>
</ul>
<blockquote>
<h2 id="window">window</h2>
</blockquote>
<p>window代表 浏览器窗口</p>
<pre><code class="language-java">window.innerHeight
920
window.innerWidth
417
window.outerHeight
1040
//可以调整窗口再试试
</code></pre>
<blockquote>
<h2 id="navigator">navigator</h2>
</blockquote>
<p>navigator,封装了浏览器的信息</p>
<pre><code class="language-javascript">navigator.appName
"Netscape"
navigator.appVersion
"5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
navigator.userAgent
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
navigator.platform
"Win32"
</code></pre>
<p>大多数时候,我们不会使用navigator对象,因为会被人为修改!</p>
<p>不建议使用这些属性来判断和编写代码</p>
<blockquote>
<h2 id="screen">screen</h2>
</blockquote>
<p>代表屏幕尺寸</p>
<blockquote>
<pre><code class="language-javascript">screen.width
1920
screen.height
1080
</code></pre>
</blockquote>
<blockquote>
<h2 id="location重要">location(重要)</h2>
</blockquote>
<p>location代表当前页面的URL信息</p>
<pre><code class="language-javascript">
</code></pre>
<pre><code class="language-javascript">host: "localhost:63342"
href: "http://localhost:63342/spring-study/%E5%89%8D%E7%AB%AF/index.html?_ijt=39pt0pkpdi1925r2i05kspsg4b"
protocol: "http:"
reload: ƒ reload() //刷新网页
//设置新的地址
location.assign('http://www.baidu.com')
</code></pre>
<blockquote>
<h2 id="document">document</h2>
</blockquote>
<p>document代表当前的页面,HTML DOM文档树</p>
<pre><code class="language-javascript">document.title
"百度一下,你就知道"
document.title = 'yinrui'
"yinrui"
</code></pre>
<p>获取具体的文档树节点</p>
<pre><code class="language-javascript"><dl id="app">
<dt>java</dt>
<dt>javaSE</dt>
<dt>javaEE</dt>
</dl>
<script>
var dl = document.getElementById('app');
</script>
</code></pre>
<p>获取cookie</p>
<pre><code class="language-javascript">document.cookie
"BAIDUID=C6990546C0F3B55F68A3111256ACE73D:FG=1; PSTM=1584319600; BD_UPN=12314753; BIDUPSID=B5F50D15A09C7E87A8F3CD1835FFA538; BDSFRCVID=ylCOJeCmHCKHnV5uAVK4heHjzeKK0gOTHllkSqftBeiEW-AVJeC6EG0Ptf8g0KubUElrogKK0gOTH6KF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tJ-qoII5JIL3fP36qRbobR3DbMnf2D6X56PsXRRnBhcqEn6SLUR1hl505tQpB5335mbBXJ5cWKJJ8UbSh-vO0n_E5-5-axQUHm6paJ5nJq5nhMJmb67JDMP0-Ro42njy523ion3vQpP-OpQ3DRoWXPIqbN7P-p5Z5mAqKl0MLPbtbb0xXj_0-nDSHHDjqTLq3H; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; delPer=0; BD_CK_SAM=1; PSINO=3; COOKIE_SESSION=3_0_9_3_53_25_1_2_9_5_0_0_155654_0_5_0_1585985674_0_1585985669%7C9%230_0_1585985669%7C1; BD_HOME=1; BDRCVFR=I67x6TjHwwYf0; H_PS_PSSID=1426_31170_21124_31186_30908_30824_31086_26350_31163_22159; sug=3; sugstore=0; ORIGIN=0; bdime=0"
</code></pre>
<p><strong>劫持cookie原理</strong></p>
<p>www.taobao.com</p>
<pre><code class="language-javascript"><script src = "aa.js"></script>
<!-- 恶意人员:获取你的cookie上传到他的服务器~ -->
</code></pre>
<p>服务器端可以设置cookie:httpOnly</p>
<blockquote>
<p><strong>history</strong></p>
</blockquote>
<pre><code class="language-javascript">history.back() //后退
history.forward()//前进
</code></pre>
<hr>
<h1 id="8操作dom对象重点">8、操作DOM对象(重点)</h1>
<blockquote>
<p><strong>核心</strong></p>
</blockquote>
<p>浏览器网页就是一个Dom树形结构!</p>
<ul>
<li>更新:更新Dom节点</li>
<li>遍历dom节点:得到Dom阶段</li>
<li>删除:删除一个Dom节点</li>
<li>添加:添加一个新的节点</li>
</ul>
<p>要操作一个Dom节点,就必须要获得这个Dom节点</p>
<blockquote>
<p><strong>获得Dom节点</strong></p>
</blockquote>
<pre><code class="language-javascript"> var h1 = document.getElementById('h1');
var p1 = document.getElementsByTagName('p1');
var p2 = document.getElementsByClassName('p2');
var father = document.getElementById('father');
var childrens = father.children;//获取所有父节点下的子节点
//father.firstChild;
//father.lastChild;
</code></pre>
<p>以上是原生代码</p>
<blockquote>
<p><strong>更新节点</strong></p>
</blockquote>
<pre><code class="language-html"><div id="id1">
</div>
<script>
document.getElementById('id1');
</script>
</code></pre>
<p>操作文本</p>
<ul>
<li>id1.innerText = '123' 修改文本的值</li>
<li>id1.innerHTML = '<strong>123</strong>' 可以解析html标签</li>
</ul>
<p>操作JS</p>
<pre><code class="language-javascript">id1.innerText = 'abc'; //属性使用字符串包裹
id1.style.color = 'red';
id1.style.fontSize = '20px'; //使用驼峰命名规则
id1.style.padding = '2em';
</code></pre>
<blockquote>
<p><strong>删除节点</strong></p>
</blockquote>
<p>删除节点的步骤:线获取父节点,再通过父节点删除自己</p>
<pre><code class="language-javascript">var id1 = document.getElementById("id1");
var father = id1.parentElement;
father.removeChild(id1);
</code></pre>
<p>注意:删除多个节点的时候,children是在时刻变化的,删除节点的时候一定要注意~</p>
<blockquote>
<p><strong>插入节点</strong></p>
</blockquote>
<p>我们获得了某个Dom节点,假设这个dom节点是空的,我们通过innerHTML就可以增加一个元素了,但是这个DOM节点已经存在元素了,我们就不能这么干了!会产生覆盖</p>
<p>追加</p>
<pre><code class="language-html"><p id="js">JavaScript</p>
<div id="list">
<p id="se">javaSE</p>
<p id="ee">javaEE</p>
<p id="me">javaME</p>
</div>
</div>
<script>
var js = document.getElementById('js');//已经存在的节点
var list = document.getElementById('list');
list.append(js);//追加到后面
</script>
</code></pre>
<blockquote>
<p><strong>创建一个新的标签,实现插入</strong></p>
</blockquote>
<pre><code class="language-html"><p id="js">JavaScript</p>
<div id="list">
<p id="se">javaSE</p>
<p id="ee">javaEE</p>
<p id="me">javaME</p>
</div>
</div>
<script>
var js = document.getElementById('js');//已经存在的节点
var list = document.getElementById('list');
//通过js创建一个新节点
var newP = document.createElement('p');//创建一个p标签
newP.id = 'newP';
newP.innerText = 'Hello.YinRui';
list.append(newP);
</script>
</code></pre>
<hr>
<h1 id="9操作表单验证">9、操作表单(验证)</h1>
<blockquote>
<p><strong>提交表单 md5加密密码 表单优化</strong></p>
</blockquote>
<pre><code class="language-HTML"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://blog-static.cnblogs.com/files/7qin/md5.js" ></script>
</head>
<body>
<!--
表单提交事件
onsubmi= 绑定一个提交检测的函数,true false
将这个结果返回给表单,使用onsubmit提交!
使用onsubmit提交可以再JavaScript中使用函数过滤表单内容
-->
<form method="post" onsubmit="return aaa()">
<p>
<span>用户名: </span><input type="text" id="username" name="username">
</p>
<p>
<span>密码: </span><input type="password" id="input-password">
</p>
<input type="hidden" id="password" name="password"> <!--这里我们使用了隐藏域提交,上面额input-password仅仅是提供用户输入-->
<!--绑定事件 onclick 被点击-->
<button type="submit">提交</button>
</form>
<script>
function aaa() {
var uname = document.getElementById('username');
var input_pwd = document.getElementById('input-password');
var md5pwd = document.getElementById('password');
console.log(uname.value);
//MD5算法
md5pwd.value = hex_md5(input_pwd.value);
console.log(pwd.value);
//可以接受判断表单内容
return true;
}
</script>
</body>
</html>
</code></pre>
<hr>
<p>以上代码可以看出,在提交表单时我们不使用<input type="submit">来提交表单数据,而在form标签中使用onsubmit属性来提交表单,这样我们就可以再表单提交时通过绑定一个函数来过滤表单内容,并且我们对表单进行了隐藏域的优化,使用户输入的密码框只承担传递值的作用,我们在提交表单时时使用隐藏域提交的,这既提高了网站的安全性,同时也在表单提交时防止密码框中内容的瞬间变长,避免导致网站实现细节的暴露。</p>
<hr>
<h1 id="10jquery">10、jQuery</h1>
<p>JavaScript</p>
<p>jQuery库,里面存在大量的JavaScript函数</p>
<blockquote>
<p><strong>获取jQuery</strong></p>
</blockquote>
<pre><code class="language-html"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- CDN引入 -->
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
</body>
</html>
</code></pre>
<blockquote>
<p><strong>选择器</strong></p>
</blockquote>
<pre><code class="language-javascript"> //选择器就是css的选择器
$('#test-jquery').click(function () {
alert('hello,jQuery!');
})
//原生js,选择器少,麻烦不好记
//标签
document.getElementsByTagName();
//id
document.getElementById();
//来
document.getElementsByClassName();
//jQuery css 中的选择器它全部都能用
$('p').click();//标签选择器
$('#p').click();//id
$('.class1').click(); //class选择器
//公式
$(selector).action();
</code></pre>
<blockquote>
<p><strong>事件</strong></p>
</blockquote>
<p>鼠标事件、键盘事件、其他事件</p>
<p><img src="https://img2020.cnblogs.com/blog/1893473/202004/1893473-20200406120128658-1872557248.png" alt="" loading="lazy"></p>
<pre><code class="language-html"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="lib/jquery-3.4.1.js"></script>
</head>
<style>
#divMove{
width:500px;
height: 500px;
border: 1px solid red;
}
</style>
<body>
<!--要求:获取鼠标当前的一个做标-->
mouse : <span id="mouseMove"></span>
<div id="divMove">这里移动鼠标试试</div>
<script>
/*当网页元素加载完毕之后,响应事件*/
$(function () {
$('#divMove').mousemove(function (element) {
$('#mouseMove').text('x:'+element.pageX+'y:'+element.pageY)
})
});
</script>
</body>
</html>
</code></pre>
<blockquote>
<p><strong>操作DOM</strong></p>
</blockquote>
<p>节点文本操作</p>
<pre><code class="language-javascript">$('#test-ul li').text(); //获取值
$('#test-ul li').text('设置值'); //设置值
$('#test-ul').html(); //获取值
$('#test-ul').html('设置值'); //设置值
</code></pre>
<p>css的操作</p>
<pre><code class="language-javascript">$('#test-ul li').css('color','red');
</code></pre>
<p>元素的显示和隐藏 : 本质 display : none;</p>
<pre><code class="language-javascript">$('#test-ul li').show();
$('#test-ul li').hide();
</code></pre>
</div>
<div id="MySignature" role="contentinfo">
Keep Clam and Carry Keen.<br><br>
来源:https://www.cnblogs.com/MrKeen/p/12626905.html
頁:
[1]