HTML-8
(一)引用数据类型
- object
- function
- array
object
JavaScript对象用花括号来书写
对象属性是name:value由逗号分隔
var x={firstname:"bill",lastname:"ddd"};
function
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
function add(a,b){
return a+b;
}
var x=add("ss","dd");
var s=add(1,2);
console.log(x);
console.log(typeof x);
console.log(s);
console.log(typeof s);
</script>
</body>
</html>
array
var arr={"aaa","ss","dd"};
特征
- 值可变
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
var x={age:20};
x.age=21;
console.log(x.age);
console.log(typeof x.age);
</script>
</body>
</html>
- 存放在栈和堆中
(二)数据类型检验方法
1.typeof
返回一个表示数据类型的字符串
- number
- boolean
- string
- symbol
- object
- undefined
- function
不能判断array和null,以及date,regexp等
2.instanceof
用来判断A是否为B的实例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
console.log([]instanceof Array);
console.log({} instanceof Object);
console.log(new Date instanceof Date);
console.log(new RegExp instanceof RegExp);
</script>
</body>
</html>
无法判断字面量初始化的基本数据类型:
console.log(1 instanceof Number);//false
console.log(new Number(1) instanceof Number);//true
3.constructor
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
console.log((1).constructor===Number);
var a=[1,2];
console.log(a.constructor===Array);
console.log(a.constructor===Object);//false
var date=new Date()
console.log(date.constructor==Date);
</script>
</body>
</html>
constructor检测Object与instanceof不一样,只认当前数据类型,不认父类
var a=[1,2];
console.log(a.constructor===Array);//true
console.log(a.constructor===Object);//false
console.log([]instanceof Array);//true
console.log([] instanceof Object);//true
4.总结
| 判断方法 | 缺点 |
|---|---|
| typeof | 不能判断array和null,以及date,regexp等 |
| instanceof | 无法判断字面量初始化的基本数据类型;null和undefined也无法被识别 |
| constructor | null和undefined也无法被识别 |
(三)运算符
- 检验密码长度(点击检验)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
密码:<input id="password" type="password" name="password" placeholder="密码长度在6-18之间">
<p><button type="button" onclick="checkpassword()">确定</button></p>
<script type="text/javascript">
function checkpassword(){
//获取密码
var password=document.getElementById("password").value;
//判断密码长度,不合格时弹框提示
if(password.length<6||password.length>18){
alert("密码长度在6-18之间,请重新输入");
}
}
</script>
</body>
</html>
- 检验用户名(时时检验)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
<style type="text/css">
span{
font-size: 10px;
color: red;
}
</style>
</head>
<body>
用户名:<input id="username" type="text" name="username" placeholder="请输入用户名" onkeyup="CheckUserName(this)">
<span id="msg"></span><br>
密码:<input id="password" type="password" name="password" placeholder="密码长度在6-18之间">
<p><button type="button" onclick="checkpassword()">确定</button></p>
<script type="text/javascript">
function checkpassword(){
//获取密码
var password=document.getElementById("password").value;
//判断密码长度,不合格时弹框提示
if(password.length<6||password.length>18){
alert("密码长度在6-18之间,请重新输入");
}
}
function CheckUserName(username){
var value=username.value;
/*使用弹窗*/
// if(value=="admin"){
// alert("不允许使用admin")
// }
/*使用插入*/
if(value=="admin"){
document.getElementById("msg").innerHTML="不允许使用admin";
}
else{
document.getElementById("msg").innerHTML="";
}
//简化
//document.getElementById("msg").innerHTML=(username.value=="admin"?"不允许使用admin":"");
}
</script>
</body>
</html>
1.算术运算符
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
var n1=100+10+"100";
var n2="100"+10+100;
console.log(n1);
console.log(n2);
console.log(typeof n1);
console.log(typeof n2);
</script>
</body>
</html>
2.loop循环
- for…in…可以写下标
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
var n1=["a","b","c","d","e"];
for(let x in n1){
console.log("第",x,"个:",n1[x]);
}
</script>
</body>
</html>
- for…of…(元素)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
var n1=["a","b","c","d","e"];
for(let x of n1){
console.log(x);
}
</script>
</body>
</html>
(四)字符串
length
length 属性返回字符串的长度:
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
查找字符串中的字符串
- indexOf() 方法返回字符串中指定文本首次出现的索引(位置):
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");//17
- lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引:
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China");//51
- 如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。
- 两种方法都接受作为检索起始位置的第二个参数。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China",18);
var po = str.indexOf("China", 3);//17
console.log(pos);
console.log(po);
</script>
</body>
</html>
- lastIndexOf() 方法向后进行检索(从尾到头),这意味着:假如第二个参数是 50,则从位置 50 开始检索,直到字符串的起点。
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China", 50);
- search() 方法搜索特定值的字符串,并返回匹配的位置:
var str = "The full name of China is the People's Republic of China.";
var pos = str.search("China");
- 两种方法,indexOf() 与 search(),是相等的。
这两种方法是不相等的。区别在于:
search() 方法无法设置第二个开始位置参数。
indexOf() 方法无法设置更强大的搜索值(正则表达式)。
提取部分字符串
有三种提取部分字符串的方法:
- slice(start, end)
- substring(start, end)
- substr(start, length)
- slice() 方法
slice() 提取字符串的某个部分并在新字符串中返回被提取的部分。
该方法设置两个参数:起始索引(开始位置),终止索引(结束位置
个例子裁剪字符串中位置 7 到位置 13 的片段:
var str = "Apple, Banana, Mango";
var res = str.slice(7,13);
如果某个参数为负,则从字符串的结尾开始计数。
这个例子裁剪字符串中位置 -12 到位置 -6 的片段:
var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7);
如果省略第二个参数,则该方法将裁剪字符串的剩余部分:
var res = str.slice(7);
substring() 方法
substring() 类似于 slice()。
不同之处在于 substring() 无法接受负的索引。
var str = "Apple, Banana, Mango";
var res = str.substring(7,13);
如果省略第二个参数,则该 substring() 将裁剪字符串的剩余部分。
substr() 方法
substr() 类似于 slice()。
不同之处在于第二个参数规定被提取部分的长度。
var str = "Apple, Banana, Mango";
var res = str.substr(7,6);
var str = "Apple, Banana, Mango";
var res = str.substr(7);
替换字符串内容
replace() 方法用另一个值替换在字符串中指定的值:
实例
str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3School")
replace() 方法不会改变调用它的字符串。它返回的是新字符串。
默认地,replace() 只替换首个匹配:
转换为大写和小写
- 通过 toUpperCase() 把字符串转换为大写:
实例
var text1 = "Hello World!"; // 字符串
var text2 = text1.toUpperCase(); // text2 是被转换为大写的 text1
- 通过 toLowerCase() 把字符串转换为小写:
实例
var text1 = "Hello World!"; // 字符串
var text2 = text1.toLowerCase(); // text2 是被转换为小写的 text1
concat() 方法
concat() 连接两个或多个字符串:
实例
var text1 = "Hello";
var text2 = "World";
text3 = text1.concat(" ",text2);
随机推荐
- Hexo博客Next主题添加粒子时钟特效
博客应用canvas粒子时钟的操作步骤: 在\themes\next\layout\_custom\目录下,新建clock.swig文件,内容如下: <div style="" ...
- ES 实战复杂sql查询、修改字段类型
转载请注明出处: 1.查询索引得 mapping 与 setting get 直接查询 索引名称时,会返回 该 索引得 mapping 和 settings 得配置,上述返回得结构如下: { &quo ...
- Django-4.2博客开发教程:需求分析并确定数据表(四)
前三步已经完成了一个初步流程,从创建项目>应用>数据迁移>访问首页.以下是我整理的基本流程,接下来一步一步完成整个项目. 1.我们的需求: 博客的功能主要分为:网站首页.文章分类.文 ...
- 【问题解决】docker版本v23.0后,构建Dockerfile中FROM私库镜像报错构建失败
问题情况 Docker版本在v23.0以后,只要Dockerfile中FROM的私库镜像不存在本地,就会报错: # 我本地是v24.0.2版本Docker [root@localhost ipd]# ...
- React Native集成CodePush热更新遇到的坑,以及折腾过程。"CFBundleShortVersionString" key needs to specify a valid semver string
最近开始一个React Native的新项目.按惯例,在创建完项目后,先集成CodePush热更新功能. 这种活已经干过不止一两次了,当然没啥问题,直接上手开干. 可问题恰恰出在了本以为应该很顺利的地 ...
- go项目实现在配置文件实现配置项统一管理
转载请注明出处: go项目中实现配置项统一管理,实现逻辑:将 配置项整理为一个json的数据结构,并保存到go.conf文件中,然后在go项目启动main方法中加载 go.conf 文件,读取go.c ...
- Module build failed: TypeError: this.getOptions is not a function at Object.loader
这个问题主要是因为node-loader版本过高导致的问题 解决方案 css-loader降为3.6.0版本即可 npm install css-loader@2.0.2 --save-dev npm ...
- java入门2..0
java的运行原理 1.在本地磁盘中创建一个文本文件为Demo.java的源文件 2.在源文件中编写java代码如下: public class Demo public static void ,ma ...
- 从壹开始前后端开发【.Net6+Vue3】
项目名称:KeepGoing(继续前进) 1.1介绍 工作后,学习的脚步一直停停走走,希望可以以此项目为基础,可以不断的迫使自己不断的学习以及成长 将以Girvs框架为基础,从壹开始二次开发一个前后端 ...
- 如何调用api接口获取到商品数据
要调用API接口获取商品数据,需要进行以下步骤: 确定API接口 首先需要确定要使用的API接口,可以通过搜索引擎或者相关文档来查找适合的API接口.以淘宝开放平台为例,可以使用淘宝的商品信息查询AP ...