字符串,数组,定时器,form

时间:2023-03-10 03:21:31
字符串,数组,定时器,form

一.字符串

    <script>
/*
// 字符串的属性
var a="hello world";
console.log(a.length);
console.log(a[1]);
var b=11;
// alert(typeof(b.toString())); // 将数字转换成字符串
// substring截取字符串 会自己比较参数的大小,小的在前面。负数转为0
console.log(a.substring(3,6)); // 通过索引截取一段字符
console.log(a.substring(6,3)); // 等同上
console.log(a.substring(-3,6)); // 负数等于零(0,6)
// slice切片一个参数
console.log(a.slice(-5)); // 切片,倒数第五个到最后
console.log(a.slice(2)); // 切片,二到最后
console.log(a.slice(3,6)); // 3-6
console.log(a.slice(-5,-1)); //
// * split切割 返回 数组,在通过索引可以拿到里面的内容
console.log(a.split("w"));
// * indexOf查找字符串 又返回索引 不存在返回 -1
console.log(a.indexOf("o")); // 返回找到的第一个字符索引
// toUpperCase将字符串全部转换成大写
console.log(a.toUpperCase());
// toLowerCase将字符串全部转换成小写
console.log(a.toLowerCase());
*/
</script>

二.数组

    <script>
// 数组array -存储数据 有序可写 var shuzu=[1,2,"a"] or var shuzu2=new Array();在通过shuzu2[0]="val"
// 查看数组的个数 arr.length
// 可以读可写 arr[0]="x";
// 在后面添加 能同时添加多个值 arr.push
// 在前面添加 能同时添加多个值 arr.unshift()
// 删除数组的最后一个 arr.pop()
// 删除数组的第一个 arr.shift() // * 数组的长度 arr.splice() 返回删除(修改)值
// 一个参数 数组的长度为1,也就是数组只保留一位,删除其他
var arr=["a",1,"d",2,3,4,5];
// console.log(arr.splice(1)); 返回值[1,"d",2,3,4,5] arr=["a"]
// 二个参数 从第一位开始删除2位
// console.log(arr.splice(1,2)); // 1,"d"
// 三个参数
// 从第0个开始,将后面2个删除再添加一个"z"
//console.log(arr.splice(0,2,"z")); // 打印改变的值(1,2) arr变成["a","z",2,3,4,5]
// 从第0个开始,将后面2个删除再添加"z","x"
//console.log(arr.splice(0,2,"z","x")); // 打印改变的值(1,2) arr变成["a","z","x",2,3,4,5]
// 第二位开始前面插入 d前面
//console.log(arr.splice(2,0,"z","x")); // 打印 [],arr变成 ["a",1,"z","x","d",2,3,4,5]
//join 将数组拼接成字符串
//将数组以 什么方式 拼接起来成为字符串,**可以识别标签
console.log(arr.join(""));
console.log(arr.join("~"));
/*
arr.sort() // 1.非数学上的顺序
顺序来排序
arr.reverse()
倒序排序
    
*/
</script>
 2.按数学上的大小排序
<script>
var arr = [-8,15,2,3,1];
arr.sort(function (a,b) {
// return a - b; //从小到大
// return b - a; //从大到小
// return 1; // 原来arr的倒序
// return 0; // 大于等于0,为原来的顺序
});
console.log(arr);
</script>

三:定时器

  时间对象:

/*
// 时间对象
// 获取时间
var date=new Date();
// 获取时间戳
// 1.
var strap = date.getTime();
// 2.
var strap2 = date*1;
// 获取年
var year = date.getFullYear();
// 获取月份 月份为0~11
var month = date.getMonth()+1;
// 获取日期 日期为1~31
var days = date.getDate();
// 获取小时
var hour = date.getHours();
// 获取分钟 分钟
var min = date.getMinutes();
// 获取秒 秒
var sec = date.getSeconds();
// 获取周几
var day=date.getDay();
*/

  定时器:

    <script>
// 定时器
// 1.延迟定时器
// 1)执行一次 setTimeout(Fn,time), Fn为执行的事件,time为时间
setTimeout(function () {
alert(1)
},2000); // 2000=2s
// 2)执行多次 setInterval(Fn,time)
setInterval(function () {
console.log(1)
},2000)
// 清除定时器
clearTimeout(name); // 定时器name
clearInterval(name) ; //定时器 name
*/
</script>

四.form表单

        <!--form表单-->
<!--action="向何处提交,将数据提交给后台处理" method="提交的方式post,get"-->
<!--get以name=value提交,没有设置value则显示on,显示在网址栏-->
<!--post提交,显示在开发者工具的 Nerwork下的Doc里面-->
<form action="" method="get" autocomplete="off"> <!--autocomplete="off"关闭提示的账号历史信息-->
<!--input是单标签-->
账号:<input type="text" name="user" placeholder="提示语句 账号"><br>
密码:<input type="password" name="paswd" placeholder="提示语句 密码"><br>
<!--单选框 为一个选项,多个选项中选一个,需要表明多个选项是一类的。通过name实现
checked 是默认选中 ,disabled禁选框-->
性别:
<input type="radio" name="gender" value="nan">男
<input type="radio" name="gender" checked value="nv">女
<input type="radio" name="gender" disabled value="no">Thailand<br>
<!--多选框 checked 是默认选中,disabled禁选框-->
选择爱好:
<input type="checkbox" id="dance" name="hobby">
<label for="dance">跳舞</label> <!--通过id关联,点击内容 跳舞也能选中多选框 -->
<input type="checkbox" name="hobby" checked>唱歌
<input type="checkbox" name="hobby" disabled>游泳<br>
<!--下拉框 后台通过name和value 拿数据-->
选择地区:
<select name="address" id="a" size="2"> <!--size显示2项出来 -->
<option value="1">成都</option>
<option value="2" selected>南极</option> <!--selected默认展示的项 -->
<option value="3">北极</option>
<option value="4">中国</option>
</select><br>
<!--文本框 通过样式设置
宽高 resize=none设置不拉伸,
=vertical竖直方向拉伸,
=horizontal水平拉伸 -->
文本框:
<textarea name="" id=""></textarea><br>
<!--提交submit默认value值提交,就是--按钮上显示的字,可以修改-->
提交按钮:
<input type="submit" value="tijiao"><br>
<!--重置 value默认 重置-->
重置:
<input type="reset">
<!--button value默认为空 js来操作-->
<input type="button" value="js操作"><br>
</form>

效果

字符串,数组,定时器,form