1. JS naming convention
Naming convention is very necessary to enhance the readability of the code. You can understand the meaning at a glance. The specification is to comply with the rules and make the code beneficial to the later stage. Maintenance can also greatly improve development efficiency. A normal website has a lot of JS code. If you don’t follow certain rules during the writing process, you won’t be able to understand what you wrote later. This is a very troublesome thing, so you should write it in your usual time. During the practice, develop good coding habits.
Usually Hungarian nomenclature or camel case nomenclature is used.
The principle of Hungarian nomenclature: variable name = attribute + type + object description. His key is: prefix with one or more lowercase letters, followed by one or more words with an initial capital letter, which specifies the purpose of the variable.
The principle of camel case naming: the first word starts with a lowercase letter, and the first letter of each subsequent word is capitalized. For example: myFirstName, myLastName. Such variable names look like camels rising one after another, hence the name. The key to the camel case method is: except for the first word, the size of the first letters of other words, a mixture of uppercase and lowercase letters to form variable names and function names, and underscores can also be used to form logical breakpoints, which can enhance the readability of the code. .
What kind of naming convention to use depends on personal preference or company regulations. Note: When naming, you cannot use reserved words or names that are too long and difficult to remember. Avoid using two similar variable names. The following are some commonly used Hungarian nomenclature prefixes:
2. Comments are very important
Single-line comments in JS are marked with "//Comment content". Multi-line comments are marked with " /* comment content */ ". The purpose of comments is to improve the readability of the code. It is not only convenient for you to use in the future, but also helps others read and understand the JS code you wrote. The content in the comments will not be displayed on the web page. To facilitate reading, comments are generally placed at or around the end of statements that require explanation. In the process of learning, we must develop a good habit of writing comments, which will help us understand the code, or mark out concepts that were vague at the time, so that we can go back and do in-depth targeted learning to have a firmer grasp of the knowledge point.
3. JS statements and symbols
JS statements are commands sent to the browser. The purpose of these commands is to tell the browser what to do. The execution rules of JS are based on line units from top to bottom. Generally, each line is a statement. For example: var a = 1+2; This statement first uses the var keyword to declare the variable a, and then assigns the operation result of 1+2 to the variable a. It should be noted here that the = symbol does not mean an equal sign in JS, but an assignment. Another example: alert('hello'); This is a JS statement. The end of a line is considered the end of the statement. Usually, a; is added at the end to indicate the end of the statement. If there are multiple lines of JS statements, each statement ends with If ;, the statements are executed in order. Note: The codes and symbols in JS must be entered in English. Although you do not need to write;, you will inevitably encounter some errors. The browser will determine that the previous sentence and the next sentence can be interpreted together, so that This resulted in some unexpected errors. We must develop good programming habits and remember to add semicolons where necessary.
JS is size-sensitive. When writing JS code, you need to pay attention to whether the case switching key is turned off.
4. JS judgment statements and for loops
If judgment statements and for loops are used very frequently in JS.
Usually when writing code, you always need to perform different actions for different decisions. You can use if conditional statements in the code to complete this task.
In JS, you can use the following conditional statements:
(1) If statement: The code is executed only when the specified condition is true, that is, when the condition is established.
(2), if...else statement: The code after if is executed when the condition is true, and the code after else is executed when the condition is not true (false).
(3), if... else if.... else statement: Use this statement to select one of multiple code blocks to execute based on the judgment condition.
Example: Prompt different greetings according to different times. When the time is less than 12 o'clock, greetings are good morning. When the time is greater than or equal to 12 o'clock and less than 18 o'clock, greetings are good afternoon. Otherwise, greetings are good evening.
<script> var d = new Date(); var time = d.getHours(); if (time < 12){ alert('早上好'); } else if (time >= 12 && time < 18){ alert('下午好'); } else{ alert('晚上好'); } </script>
If you want to run the same code over and over again, and the value is different each time, it is very convenient to use a loop. When there is a group of elements, you can use a for loop to Element addition event.
Syntax of for loop:
for (语句 1; 语句 2; 语句 3){ 被执行的代码块 } 语句1用于初始化循环中所有的变量。通常为:var i=0; 语句2用于设置初始变量的条件。通常为:i<object.length; 语句3用于增加初始变量的值。通常为:i++ 也可以为:i--
Example: Loop through the data in the array and output in sequence:
//在不使用循环时,我们可以这样输出数组中的值: var cars=["路虎","宝马","奔驰","奥迪","别克","大众"]; document.write(cars[0] + "<br>"); document.write(cars[1] + "<br>"); document.write(cars[2] + "<br>"); document.write(cars[3] + "<br>"); document.write(cars[4] + "<br>"); document.write(cars[5] + "<br>"); //使用for循环可以很简洁的完成输出: for (var i=0, i<cars.length; i++){ document.write(cars[i] + "<br>"); }
document.write() 可用于直接向 HTML 输出流写内容,可以在平时做练习时用于向页面输出内容测试代码,这里需要注意 document.write() 一定要向文档输出写内容,如果文档已经加载完成后,再来执行该语句,则整个页面将会被覆盖。如果 document.write() 放在事件中,则会先清空页面上所有的内容,然后再写入内容。
5、JS 一些基本概念
(1)、标识符:标识符是 JS 中定义的符号,可以由任意顺序的大小写字母、数字、下划线、和美元符号( $ )组成。标识符就是识别一个具体对象的名称,最常见的标识符就是变量名和函数名,JS对大小写敏感,所以 a 和 A 是两个不同的标识符。标识符不能以数字开头,也不能是JS中的保留关键字,具体可百度参考资料。另外还有三个虽然不是保留字,但是因为他们具有特别的含义,也不能用作标识符:Infinity、NaN、undefined。
(2)、代码块:代码块其实不难理解,先来解释以下 JS 代码,JS 代码就是 JS 语句的序列,浏览器依据编写 JS 语句的顺序依次逐行的执行每个语句,而代码块则是 JS 语句的组合,并且包含在花括号内,代码块的作用是告诉浏览器这些代码一起执行语句序列。JS 函数就是将语句组合在块中的典型例子。
(3)、变量:从字面上来理解,变量就是可以改变的量,但是从编程角度讲,变量是用于存储某种/某些数值信息的“容器”,简单说就是对“值”的引用,使用变量等同于引用一个值,每一个变量都有一个变量名。比如:var x = 1; 先声明一个变量 x,x 就是变量名,然后用 = 赋值,也就是将 1 赋值给 x,以后,在引用 x 的时候,就会得到值 1。在 JS 中创建一个变量通常被称为“声明”一个变量,var 就是用来声明变量的。变量在声明之后是空的,他的值为undefined(未定义),需要给他赋值后使用,= 就是建立这种引用关系。上面的代码可以看作为:var x;x=1;这样写是先声明一个变量,再给其赋值,上面的代码则在声明的同时就进行了赋值。在一条语句中,可以声明多个变量,并使用逗号隔开。注意:在给变量命名的时候不能使用 JS 关键词和 JS 保留词。
(4)、常量:有变量就会有常量,变量可以理解为变化的量,而常量就是不变的量。其实在 JS 中并没有常量这个概念,所谓的常量,只不过是提前赋值的变量而已。常量和变量都属于变量,只不过常量是赋过值后就不能再改变的变量,而普通的变量可以再次进行赋值操作。为了与变量有所区分,增强代码的可读性,所以在声明一个常量时,一般采用常量名全部大写,若有多个单词,可用下划线隔开。
(5)、字面量:所谓的字面量,其实是对象的表示形式,或者说是创建方式,他不是一种值,而是一种表示值的记法,简单说字面量就是如何表达一个对象的值,在给变量赋值时,赋值运算符后面的都可以认为是字面量。字面量也叫直接量,一个字面量,也可以被认为是一个常量,如 100。这里需要注意:变量是一个名称,而字面量是一个值。字面量可分为:数字字面量、字符串字面量和表达式字面量。数字字面量,可以是整数或者是小数,比如:var a=10; var b=3.14; 10 和 3.14 就是数字字面量。字符串字面量,使用引号包含起来的一系列字符,比如:var str='小明'; '小明'就是字符串字面量。表达式字面量又可分为:数组字面量、对象字面量、函数字面量。数组字面量,每个值用逗号隔开,比如:var arr = [1,2,3,4,5,6]; [1,2,3,4,5,6] 就是数组字面量。对象字面量是一个键值对的组合,每个键值对之间用逗号分割,并包含在花括号内,比如:var obj = {a:12, b:5, c:'21'}, {a:12, b:5, c:'21'} 就是对象字面量。函数字面量,function myFunction(a, b) { return a * b;} 函数字面量是用关键字 function 后加可选的函数名、用圆括号括起来的参数和用花括号括起来的执行语句构成。函数字面量是一个表达式,而不是语句。上面的例子可写为:var myFunction = function (a, b) { return a * b;} ,这样就有便于理解了,也可以说函数的字面量就是一个匿名函数,他的指向是归谁使用。
(6)、变量的作用域:变量分为:全局变量和局部变量。全局变量简单说就是在函数外声明的变量,任何地方都可以使用,而局部变量就是在函数内部声明的变量,只能在声明他的函数内部使用。这就是变量的作用域,通俗理解就是他的作用范围。JS 变量的生命期从他们被声明的时候开始,全局变量会在页面被关闭之后删除,局部变量则在函数被运行以后删除。
(7)、表达式:表达式与数学中的定义相似,是指具有一定的值、用运算符把常量和变量连接起来的代数计算式,一个表达式可以包含常量或变量。比如:var a=a+1; a 是变量名称,a+1 就是表达式。在生活中”再见“表达方式有很多种,比如:英语(goodbey),网络语(886),肢体语言(摆摆手)等。JS 中的表达式无处不在,可以表达以下几种内容:字符串的连接,被称为串表达式,var str = "I" + "love" + "you"; 还有这种形式:var str = "hello"; var str1 = str + "World"; 串表达式值为字符串。数值表达式,var num = 10 + 2.5; 也有如下形式:var num = 10 + 2.5; var num1 = num + 10 * 2.5; 数值表达式值为数值。还是有一种是布尔表达式,var num = 2; alert(num == 2); 返回 ture,alert(num > 10); 返回 false。布尔表达式值为 true 或 false。
(8)、函数:一看到函数,很多人可能就头疼,但是编程中函数还是很好理解的,是由事件驱动的或者当它被调用时执行的可重复使用的代码块。简单说就是完成某个特定功能的一组语句,使用function关键字定义包裹在花括号中的代码块,便于反复调用。其基本形式为:function moveStart(){代码} 。声明一个函数只是在告诉浏览器这有一个函数,不会被实际执行,而在函数调用的时候,才真正执行函数内的代码。moveStart 就是函数名,遵循 JS 命名规范,在函数调用的时候使用:moveStart()。若将函数定义在变量后,变量则可作为函数使用。函数存储在变量中,不需要函数名,可以通过变量名调用。我们把没有名称的函数称为匿名函数。函数返回值,函数执行的结果就是函数返回值,return 可以将函数内的结果返回到函数外调用,在使用 return 语句时,函数会停止执行,并返回指定的值,再从调用的地方开始执行,函数调用会被返回值取代,同样一个函数应该只返回一种类型的值,在仅仅是希望退出函数时,也可以使用 return,返回值是可选的,比如下面的代码:如果 a 大于 b,则退出函数,并不会计算 a 和 b 的和。
function myFunction(a, b){ if (a > b){ return; } x = a+b; }
(9)、函数传参:在调用函数时,可以向其传递值,这些值被称为参数,这些参数可以在函数中使用,可以传递任意多的参数,并由逗号分割,比如:function myFunction(x, y){return x * y;} ,在调用的时候传入值:myFunction(3 ,4),则返回 x乘y 的运算结果。简单理解,参数就是一个占位符,即先把位置占住,后面再用。传递的值必须与参数以一致的顺序出现,第一个值就是第一个被传递的参数给定的值,依次类推。函数很灵活,可以传递不同的值,来调用该函数。函数参数可分为:显式参数和隐藏参数( arguments )。函数的显式传参,也叫固定参,就是在函数被声明定义的时候列出的参数。隐藏参数也叫可变参,或者不定参,arguments 对象是 JS 内置的对象,对象包含了函数调用的参数数组,通过这一点可以很方便的找到最后一个参数的值,或者计算所有参数的和。当函数里有一部分定不下来的时候可以用函数传参,举一个简单的例子:点击按钮改变 DIV 的样式:
<style> #div1{ width:200px; height:200px; background:red; } </style> <body> <div id="div1"></div> <input type="button" value="变宽" onclick="setWid('width','400px')" /> <input type="button" value="变高" onclick="setWid('height','400px')" /> <input type="button" value="变绿" onclick="setWid('background','green')" /> <script> function setWid(name, value){ //函数传参 var x = document.getElementById('div1'); x.style[name] = value; } </script> </body>
1 //参数求和: 2 <script> 3 function sumArr() { 4 var result = 0; 5 for(var i = 0; i < arguments.length; i++) { 6 result += arguments[i]; 7 } 8 return result; 9 } 10 alert(sumArr(3,5,2,12,8,51,99)); //和为:18011 </script>
(10)、对象:在现实生活中,一个人可以被视为一个对象,对象有他的属性,比如性别、身高、年龄,籍贯等,方法有:走、跑、跳等。所有的人都有这些属性,但是每个人的属性又不尽相同,所有的人都有这些方法,但是他们执行的时间都不尽相同。在 JS 中,对象就是拥有属性和方法的数据,JS 中的所有事物都是对象:字符串、日期、数组、数字等等。可以说在 JS 中一切皆对象,属性是与对象相关的值,方法是能够在对象上执行的动作。简单说对象只是带有属性和方法的特殊数据类型。在 JS 中对象是数据(变量),拥有属性和方法,当在声明一个变量时,var txt = "你好"; 实际上已经创建了一个字符串对象,该对象拥有 length 属性,字符串对象同时拥有很多个内置的方法,比如:charAt() 可获取某个字符,返回字符串的某一位的字符。可以说 JS 对象是变量的容器,但是,通常认为 JS对象是键值对的容器,键值对的写法为:name:value,键与值以冒号分隔,键值对又常被称为对象属性,所以 JS 对象就是属性变量的容器。JS支持自定义对象,可以通过 new 关键字创建。
6、JS 数据类型
JS 数据类型可分为基本数据类型和复合数据类型两种,其中基本数据类型有五种:字符串(String)、数字(Number)、布尔(Boolean)、空(Null)、未定义(Undefined)。复合数据类型即 Object,Object 本质是一组无序的名值对组成的,严格来说,又被分为三类:对象(Object)、数组(Array)、函数(function)。
(1)、字符串:字符串是存储字符的变量。比如:var a='小明';,字符串可以是引号中的任意文本,可以使用单引号或者双引号。
(2)、数字:数字就是用来存储数字的变量。可以为整数也可以是小数。比如:var a1=314; var a2=3.14;。
(3)、布尔:布尔值是 "ture" 真和 "false" 假两个特定值,布尔常用在条件测试中。
什么是真 ture:非零数字、非空字符串、非空对象
什么是假 false:数字零、空字符串、空对象(null)、undefined
(4)、Null:null 是一个只有一个值的特殊类型,表示一个空对象引用,可以用来清空变量。
(5)、Undefined:表示为定义,有两种情况:1:真的没有定义。2、虽然定义了,但是没有赋值。
(6)、Object:对象由大括号包含。在括号内部,对象的属性以名称和值对的形式{name : value}来定义。属性由逗号分隔,包括了除数字、字符串和布尔值以外的所有类型。比如:var person = {name:"小明", sex:"男", id:5566};,此例子中的对象(person)有三个属性:name, sex,id。
(7)、Array:使用单独的变量名来存储一系列的值。创建一个数组的方法:var arr = new Array();,之后可以给数组添加值:arr[0] = 1; arr[1] = 2; arr[2] = 3;,数组的下标是基于零的,所以从 0 开始算起。在 JS 中,很多时候,要避免使用 new 关键字,所以将数组创建为:var arr = [1,2,3] 的形式。
(8)、function:函数其实是处理数据的方法,JS 将函数视为一种数据类型,可以像其他数据类型一样,进行赋值和传递,这就为编程带来了很大的灵活性。
变量的类型:变量本身是没有类型的,取决于他里边存储的什么数据类型,存的什么类型就是什么类型的变量。当声明新变量时,可以使用 new 来声明其类型,比如:var userName = new String; var x = new Number; var y = new Boolean; var cars = new Array; var person = new Object; 。JS 变量均为对象,当声明一个变量时,就创建了一个新的对象。
typeof运算符:typeof 操作符用来检测变量的数据类型,返回一个字符串。字符串、数字、布尔值分别返回 string、number、boolean。用 typeof 检测 null 返回 object,在 JS 中 null 表示什么都没有,但是他是一个对象。undefined 是没有赋值的变量,所以 typeof 一个没有值的变量会返回 undefined。typeof 一个对象则返回 object。JS 中数组是一种特殊类型的对象,所以也返回 object。函数则返回 function。
undefined 和 null 的区别:typeof undefined,返回 undefined。typeof null,返回 object。(null === undefined),返回 false,(null == undefined),返回true。
7、JS 运算符
(1)、算数运算符:
除了平时常见的加减乘数之外,JS 中还有一些其他的运算符:
Modulo operator: %, the popular understanding of modulo is to take the remainder, for example: 5%2, the value is 1, 5 divided by 2, the quotient of 2 will leave 1.
Auto-increment operator: ++, auto-increment operation is divided into two situations, one is to assign value first and then operate, and the other is to perform operation first and then assign value. Example: Suppose a = 2
Assign value first and then operate: var b = a++; The operation result is: b=2, a=3 Analysis: b = a, a = a+1.
Operation first and then assignment: var b = ++a; the operation result is: b=3, a=3 Analysis: a = a+1, b = a.
Through the above example, you can see that assigning a value first and then calculating it is essentially to assign a to b first, and then add 1 to it. The operation is performed first and then the value is assigned. The essence is to add 1 first and then assign the value to b.
The similarities between them are that they all increase by 1, a = a+1, and are both operated as a whole expression, that is, (a++)(++a). Although their values increment by 1, But (a++) takes the value of a before it is incremented, and (++a) takes the value of a after it is incremented.
Decrement operator: --, the decrement operator is the same as the increment operator, that is, it decrements by 1 each time. It is also divided into two situations: Example: Assume a=2
Assign value first and then operate: var b=a--; The operation result is: b=2, a=1 Analysis: b=a, a= a-1.
Operation first and then assignment: var b=--a; the operation result is: a=1, b=1 Analysis: a=a-1, b=a.
The + operator can be used to connect string variables. If you need to connect multiple strings, you can use the + operator.
If you add a string and a number, you get a string. For example: var str='hello'+2; Return: hello2
(2) Assignment operator:
The assignment operator is used to assign a value to a variable. There are the following types: = += -= *= /= %=
Example: Suppose a=10 b=5
=: a = b Result: 5
+=: a += b, equivalent to: a = a+b Result: 15
-=: a -= b, equivalent to: a = a-b Result: 5
=: a *= b, equivalent to: a = a*b Result: 50
/=: a /= b, equivalent to: a = a/b Result: 2
% =: a % b, equivalent to: a = a/b Result: 0 (the modulo operation is to take the remainder)
(3) Comparison operator:
Comparison operator in condition Used in statements to determine the relationship between variables or values and return true or false.
Comparison operators include the following: == === != !== > < >= <=
Example: Suppose a=2
==:Equal. Note that double equal signs only mean equal in JS, and one equal sign means assignment. Compare: a == 2, return true. a == 1, returns false.
===: Absolute equality, both value and type are equal. Compare: a === '2', return false. a === 2, returns true.
!=: Not equal to. and equal to the opposite. Comparison: a != 2, returns false, a != 1, returns true.
!==: Absolute inequality, the opposite of absolute equality, values and types are not equal. Comparison: a !== '2', returns true, a !== 2, returns false.
>: Greater than. Comparison: a>5, returns false.
:Less than. Compare: a<5, return true.
>=: Greater than or equal to. Comparison: a>=5, returns false.
<=: Less than or equal to. Compare: a<=5, return true.
(4) Logical operators:
Logical operators are used to describe the logical relationship between variables or values.
Logical operators include the following: && || !
&&: And. In mathematics, we express b greater than a and b less than c as: aa && b
||: Or. When any one of the two conditions is met, the result of the logical OR operation is true. Example: a=5, b=10, judge c=a
!:no. Also called the logical NOT operator, right and wrong are reversed. For example, Xiao Ming bought a JS book. Xiao Bai said: it is a turtle book; Xiao Hong said: it is a rhinoceros book. Xiao Ming said: What Xiaobai said is not the truth, and what Xiaohong said is not a lie. Then Xiao Hong was right, the book Xiao Ming bought was a rhinoceros book. Example: a=10 b=5, judge c =! (a>b), the value of c is: false.
(5) Ternary operator:
The ternary operator is also called the conditional operator. Its basic form can be expressed by if judgment statement. The ternary operator is expressed as: ? :.
所谓三元运算符,顾名思义就是需要进行三次操作,语法为:条件?结果1:结果2 。条件写在问号之前,后面跟着用冒号分隔的结果1和结果2,当满足条件时为结果1,否则就是结果2。好比你去看演唱会,条件就是需要入场券,若你带了就可以直接进去,如果没带那就请回吧,用三元运算符可表示为:带没带入场券 ? 带了直接进去 : 没带那就请回吧。
所有使用 if 判断语句的地方,都可以使用三元运算符,使用 if 判断语句代码比较繁琐,使用三元运算符,代码则十分简洁,但是对于初学者来说,三元运算符不是那么直观,使用 if 判断语句更容易理解,随着学习的不断深入,以及理解的加深,就可以使用三元运算符代替 if 了。
实例:判断一个数为偶数还是奇数。假设:a=12
先用if判断语句表示:
var a = 12; if(a%2 == 0){ alert('a为偶数'); } else{ alert('a为奇数'); }
用三元运算符可表示为:
var a = 12; a%2===0 ? alert('偶数') : alert('奇数');
(6)、运算符之间的优先级:
从高到低依次为:算术操作符 → 比较操作符 → 逻辑操作符 → "="赋值符号
同级的运算是按从左到右依次进行运算,若有括号,从多层括号由里向外进行运算。
实例:
var numA = 2; var numB = 5; var numC = numA + 40 > 10 $$ numB * 2 < 20; var numD =( ( numA + 40 ) / ( 12 - numB ) ) * 10; alert(numC); //返回:ture alert(numD); //返回:60
8、JS 事件
HTML 事件是发生在 HTML 元素上的事情。当在 HTML 页面中使用 JS 时,JS 可以触发这些事件。事件可以是浏览器行为,也可以是用户操作行为,用户操作行为可分为鼠标操作和键盘操作。比如:在页面加载完成时触发事件,这属于浏览器行为,用户的操作行为如点击按钮触发事件,鼠标的移入移出,按键提交信息等。
下面是一些常用的事件列表:
9、JS 常用的互动方法
在 JS 中可以创建三种类型的消息框:警告框、确认框、提示框。
(1)、警告框
警告框常用于确保用户可以得到某些信息,平时我们在访问网页的时候,有时突然弹出一个小窗口,上面写这一些提示文字,这就是警告框,在警告框出现之后,用户必须点击确定按钮后才能继续操作,否则就不能对网页做出任何操作。这样的弹出窗口是用”alert“实现的。
语法:alert(str或var);
(2)、确认框
确认框常用于验证用户是否接受操作,允许用户做出选择,当确认框弹出时,用户可以点击”确定“或者”取消“来确定用户操作,当用户点击确定时,返回值为 ture,如果点击取消,则返回值为 false。用户在点击对话框之前不能进行任何操作。这样的弹出窗口是用”confirm“实现的。
语法:confirm(str);
(3)、提示框
提示框也叫提问框,用于提示用户在进入页面前输入某个值,主要是询问一些需要与用户交互的信息,提示框包含一个确定、取消按钮,和一个文本框,当提示框弹出后,用户需要输入某个值,然后点击确定或者取消才能继续操作,当用户点击确认时,返回值为文本框输入的值,如果用户点击取消,则返回值为null。提示框可以和确认框相互配合使用。这样的弹出窗口是用”prompt“实现的。
语法:prompt(str1, str2);
str1 为要显示在对话框的值,也就是与用户交互的内容描述,不可修改。str2 为文本框中提示用户输入的默认内容,可以修改。
一般情况下,在实际的网站中,这些对话框都很少或者说几乎不使用,尤其是警告框,因为用户会反感这种突然弹出来的东西,在弹出对话框后用户不点击按钮之前,浏览器就跟死了一样,不能做任何操作,确认框和提示框相对来说还有一些使用,也是比较有意义的,比如页面中一个跳转按钮,如果不使用确认对话框,则直接跳转到新页面,使用确认对话框之后,用户可以点击确认或者取消来执行对应操作,确认则跳转新页面,取消则继续浏览,有时候这个跳转按钮或者链接是用户不小心点到的,并不希望跳转。大部分网站中,都使用的是一套自定义的对话框,更友好的显示。
10、JS 转义字符
In JS, strings are usually enclosed in single quotes or double quotes, for example: var txt = "Who saw that "Xiao Ming" came to class today?"; So this example will parse errors, starting from "Who saw" Truncated, the browser reports an error message: Syntax error, and this is a very serious error. If the error occurs, the code will be terminated and the subsequent code cannot be executed. In fact, this example is easy to solve. We can enclose it in single quotes and add double quotes to Xiao Ming. OK, it is the same as the original expression, but what if we need to wrap it in a new line? At this time, you need to use escape characters. Escape characters are widely used in JS, especially in regular expressions (RegExp). Therefore, the above code should be written as: var txt = "Who saw\"Xiao Ming\ "Did you come to class today?"; If you need to break the line, write it as: var txt = "Who saw \"Xiao Ming\" \n Did you come to class today?";.
Backslash (\) is an escape character. The escape character can be used to convert special characters into string characters. It can be used to escape apostrophes, quotation marks, newlines and other special characters. When writing You need to pay attention to special characters when coding. Many runtime problems are caused by special characters. Since the backslash itself is used as an escape character, you cannot just type a \ in the code. If you need to display a backslash For slashes, you must type two \\ at a time. The following are some commonly used escape characters:
The above is the content of JavaScript learning summary [2] JS basics. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!