ps设计网站首页界面,织梦网站后台管理教程,软件著作权申请,乐山公司网络建设字符串
可以使用 索引 位置访问字符串中的每个字符
可以使用内置属性 length 来计算字符串的长度
var txt ABCDEFGHIJKLMNOPQRSTUVWXYZ;
var sln txt.length;scriptvar x John; // x是一个字符串// 使用 new 关键字将字符…字符串
可以使用 索引 位置访问字符串中的每个字符
可以使用内置属性 length 来计算字符串的长度
var txt ABCDEFGHIJKLMNOPQRSTUVWXYZ;
var sln txt.length;scriptvar x John; // x是一个字符串// 使用 new 关键字将字符串定义为一个对象var y new String(John); // y是一个对象document.write(x | y) // John | Johndocument.write(br) // string | objectdocument.write(typeof x | typeof y)/scriptp iddemo/pscriptvar x John; // x 是字符串var y new String(John); // y 是一个对象document.getElementById(demo).innerHTML x y;/scriptp 为绝对相等即数据类型与值都必须相等。/p特殊字符
代码输出\’单引号\双引号\\反斜杠\n换行\r回车\ttab(制表符)\b退格符\f换页符
字符串属性
属性描述constructor返回创建字符串属性的函数 length 返回字符串的长度prototype允许您向对象添加属性和方法
字符串方法
方法描述charAt()返回指定索引位置的字符charCodeAt()返回指定索引位置字符的 Unicode 值concat()连接两个或多个字符串返回连接后的字符串fromCharCode()将 Unicode 转换为字符串indexOf()返回字符串中检索指定字符第一次出现的位置lastIndexOf()返回字符串中检索指定字符最后一次出现的位置localeCompare()用本地特定的顺序来比较两个字符串match()找到一个或多个正则表达式的匹配replace()替换与正则表达式匹配的子串search()检索与正则表达式相匹配的值slice()提取字符串的片断并在新的字符串中返回被提取的部分split()把字符串分割为子字符串数组substr()从起始索引号提取字符串中指定数目的字符substring()提取字符串中两个指定的索引号之间的字符toLocaleLowerCase()根据主机的语言环境把字符串转换为小写只有几种语言如土耳其语具有地方特有的大小写映射toLocaleUpperCase()根据主机的语言环境把字符串转换为大写只有几种语言如土耳其语具有地方特有的大小写映射toLowerCase()把字符串转换为小写toString()返回字符串对象值toUpperCase()把字符串转换为大写trim()移除字符串首尾空白valueOf()返回某个字符串对象的原始值
模板字符串
JavaScript 中的模板字符串是一种方便的字符串语法允许你在字符串中嵌入表达式和变量 模板字符串使用反引号 作为字符串的定界符分隔的字面量 模板字面量是用反引号分隔的字面量允许多行字符串、带嵌入表达式的字符串插值和一种叫带标签的模板的特殊结构
不使用转义符可以直接显示 引号 p iddemo/pscriptlet text Hes often called Runoob;document.getElementById(demo).innerHTML text;/scriptHe’s often called “Runoob” 可支持多行输入行与行之间插入一个空格 scriptconst multiLineText This isa multi-linetext.;document.write(multiLineText);/scriptThis is a multi-line text. 占位符 p iddemo/pscriptconst name Runoob;const age 30;const message My name is ${name} and Im ${age} years old.;document.getElementById(demo).innerHTML message;/scriptMy name is Runoob and I’m 30 years old. scriptlet price 10;let VAT 0.25;// toFixed 将一个浮点数转换为指定小数位数的字符串let total Total: ${(price * (1 VAT)).toFixed(2)};document.write(total);/scriptTotal: 12.50 scriptlet header ;let tags [RUNOOB, GOOGLE, TAOBAO];let html h2${header}/h2ul;for (const x of tags) {html li${x}/li;}html /ul;document.write(html)/script运算符
运算符举例% 取余5 % 2 1 自增y 5, x y → x6y6 y 5, x y → x5y6-- 自减y 5, x --y → x4y4 y 5, x y-- → x5y4
不论是y y y-- --y y本身的值都会改变
数字与字符串相加返回字符串
比较 和 逻辑运算符
比较和逻辑运算符用于测试 true 或者 false
比较运算符 x 5
运算符描述比较返回值等于x8false绝对等于值和类型均相等x“5”x5falsetrue!不等于x!8true!不绝对等于值和类型有一个不相等或两个都不相等x!“5” x!5true false大于x8false小于x8true大于或等于x8false小于或等于x8true if (age18) x“Too young”; 逻辑运算符 x 6 y 3
运算符描述例子and(x 10 y 1) 为 true||or(x 5 || y5) 为 false!not!(xy) 为 true variablename(condition)?value1:value2 p点击按钮检测年龄。/p年龄:input idage value18 /p是否达到投票年龄?/pbutton onclickmyFunction()点击按钮/buttonp iddemo/pscriptfunction myFunction() {var age, voteable;age document.getElementById(age).value;voteable (age 18) ? 年龄太小 : 年龄已达到;document.getElementById(demo).innerHTML voteable;}/scriptif 条件语句 p点击这个按钮获得基于时间的问候。/pbutton onclickmyFunction()点击这里/buttonp iddemo/pscriptfunction myFunction() {var x ;var time new Date().getHours();if (time 20) {x Good day;}else {x Good evening;}document.getElementById(demo).innerHTML x;}/scriptscript typetext/javascriptvar d new Date();var time d.getHours();if (time 10) {document.write(b早上好/b);}else if (time 10 time 20) {document.write(b今天好/b);}else {document.write(b晚上好!/b);}/scriptswitch 条件语句
选择要执行的多个代码块之一
break 阻止代码自动地向下一个 case 运行 default 关键词 规定匹配不存在时做的事情 p点击下面的按钮会显示出基于今日日期的消息/pbutton onclickmyFunction()点击这里/buttonp iddemo/pscriptfunction myFunction() {var x;var d new Date().getDay();switch (d) {case 6: x 今天是星期六;break;case 0: x 今天是星期日;break;default:x 期待周末;}document.getElementById(demo).innerHTML x;}/script注意 Sunday0, Monday1, Tuesday2, 等等
for 循环
for 循环的用法 for (语句 1; 语句 2; 语句 3) { 被执行的代码块 } scriptcars [BMW, Volvo, Saab, Ford];for (var i 0; i cars.length; i) {document.write(cars[i] br);}/scriptp点击按钮循环代码5次。/pbutton onclickmyFunction()点击这里/buttonp iddemo/pscriptfunction myFunction() {var x ;for (var i 0; i 5; i) {// 累加x x 该数字为 i br;}document.getElementById(demo).innerHTML x;}/script可以同时声明多个变量用逗号隔开
for (var i0,lencars.length; ilen; i)在循环开始前已经设置了变量值时可以省略语句1 scriptcars [BMW, Volvo, Saab, Ford];var i 2, len cars.length;for (; i len; i) {document.write(cars[i] br);}/script循环内部有相应代码时语句3也可以省略 scriptcars [BMW, Volvo, Saab, Ford];var i 0, len cars.length;for (; i len;) {document.write(cars[i] br);i;}/scriptfor in p点击下面的按钮循环遍历对象 person 的属性。/pbutton onclickmyFunction()点击这里/buttonp iddemo/pscriptfunction myFunction() {var x;var txt ;var person { fname: Bill, lname: Gates, age: 56 };// x is key 键for (x in person) {txt txt person[x] ;}document.getElementById(demo).innerHTML txt;}/scriptwhile 循环 button onclickmyFunction()点击这里/buttonscriptfunction myFunction() {var x , i 0;while (i 5) {x x i ;i;}document.write(x);}/script0 1 2 3 4 do/while 循环
该循环至少会执行一次即使条件为 false 它也会执行一次因为代码块会在条件被测试前执行 p点击下面的按钮只要 i 小于 5 就一直循环代码块。/pbutton onclickmyFunction()点击这里/buttonp iddemo/pscriptfunction myFunction() {var x , i 0;do {x x 该数字为 i br;i;}while (i 5)document.getElementById(demo).innerHTML x;}/scriptscriptcars [BMW, Volvo, Saab, Ford];var i 0;while (cars[i]) {document.write(cars[i] br);i;}/scriptbreak 和 continue
break 语句用于跳出循环 continue 用于跳过循环中的一个迭代 p点击按钮测试带有 break 语句的循环。/pbutton onclickmyFunction()点击这里/buttonp iddemo/pscriptfunction myFunction() {var x , i 0;for (i 0; i 10; i) {// if (i 3) {// break;// }// 由于这个 if 语句只有一行代码所以可以省略花括号if (i3) break;x x 该数字为 i br;}document.getElementById(demo).innerHTML x;}/scriptp点击下面的按钮来执行循环该循环会跳过 i3 的数字。/pbutton onclickmyFunction()点击这里/buttonscriptfunction myFunction() {var x , i 0;for (i 0; i 10; i) {// if (i 3) {// continue;// }if (i 3) continue;x x i ;}document.write(x);}/script0 1 2 4 5 6 7 8 9 p iddemo点击下面的按钮来执行循环该循环会跳过 i3 的数字。/pbutton onclickmyFunction()点击这里/buttonscriptfunction myFunction() {var x , i 0;while (i 10) {if (i 3) {i; //加入i不会进入死循环continue;}x x 该数字为 i br;i;}document.getElementById(demo).innerHTML x;}/script标记 JavaScript 语句在语句之前加上冒号 scriptcars [BMW, Volvo, Saab, Ford];list: {document.write(cars[0] br);document.write(cars[1] br);document.write(cars[2] br);// 跳出 JavaScript 代码块break list;document.write(cars[2] br);document.write(cars[3] br);document.write(cars[4] br);}/scriptBMW Volvo Saab