数组的应用
1.数组
代码如下(示例):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// let arr = [];
// arr.push({ name: "张三", age: 18, sex: "男" })
// arr.push({ name: "张三1", age: 19, sex: "女" })
// arr.push({ name: "张三2", age: 10, sex: "男" })
// arr.push({ name: "张三3", age: 50, sex: "男" })
//排序的规则
// arr.sort((v1, v2) => {
// return v1.age - v2.age;
// });
// let arr1 = arr.filter((item) => {
// return item.age >= 18 && item.sex === "男"
// })
let arr1 = arr.find((item) => {
return item.age >= 18 && item.sex === "男"
})
console.log(arr1)
// let arr = [];
// 在数组的末尾追加元素
// arr.push(1)
// arr.push(2)
// arr.push(3)
// arr.push(4)
// arr.push(5)
// 删除数组末尾的元素
// arr.pop()
// 往数组的开头位置插入元素
// arr.unshift(5);
// 删除数组的第一位元素
// arr.shift()
// //删除数组指定的元素 第一参数值删除元素的下标 第二参数删除的个数
// arr.splice(1, 2)
// //拼接数组
// let arr1 = ["a", "b", "c", "d"];
// let arr2 = arr.concat(arr1);
// console.log(arr2.indexOf("a"));
// //对数组进行排序
// arr2.sort()
// console.log(arr2);
</script>
</body>
</html>
2.注册
代码如下(示例):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用户注册</title>
<style>
* {
margin: 0;
padding: 0;
}
table {
display: block;
margin: auto;
width: 258px;
width: 327px;
}
table,
table td,
table th {
border: 1px solid #ccc;
border-collapse: collapse;
}
</style>
</head>
<body>
<table>
<tr>
<td>用户名:</td>
<td><input type="text"> <span>用户已存在</span></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="" id=""></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="" id=""> </td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="" id=""> </td>
</tr>
<tr>
<td>昵称:</td>
<td><input type="password" name="" id=""> </td>
</tr>
<tr>
<td>性别</td>:</td>
<td><input type="radio" name="sex" id="">男<input type="radio" name="sex" id="">女 </td>
</tr>
<tr>
<td colspan="2">
<button style="float: right;margin-right: 50px;width: 100px;">注册</button>
</td>
</tr>
</table>
</body>
</html>
该处使用的url网络请求的数据。
3.事件
代码如下(示例):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#div {
width: 500px;
height: 500px;
background: pink;
}
</style>
</head>
<body>
<!-- <form action="http://www.baidu.com" method="get" onsubmit="return check()"> -->
<div id="div"></div>
<input type="text" id="input" value="已经存才的内容">
<select name="" id="select">
<option value="">1</option>
<option value="">2</option>
</select>
<button id="btn">点击获取焦点</button>
<input type="submit" value="登录">
<!-- </form> -->
<script>
function check() {
return true;
// return false;
}
/*事件的三种绑定方式
第一种:直接在标签绑定
第二种:通过js的on方式去绑定
第三种:通过监听事件绑定
*/
//当鼠标移至某个元素时响应一次
document.getElementById("div").addEventListener("mouseover", function () {
console.log(1);
})
//当鼠标离开某个元素时响应一次
document.getElementById("div").addEventListener("mouseout", function () {
console.log(2);
})
//当鼠标在某个元素上移动时频繁响应
document.getElementById("div").addEventListener("mousemove", function () {
console.log(3);
})
//当键盘按下去升上来时触发 经常会使用到event参数
document.addEventListener("keyup", function (e) {
console.log(e.key);
})
//onload事件一般作用在window 意思当页面元素加载完成后执行
window.onload = function () {
console.log("加载完毕");
// document.getElementById("input").focus();
}
//当窗体大小改变时调用该方法
window.onresize = function () {
alert("窗体改变")
}
//当前表单元素(文本框 文本域 密码框)
// document.getElementById("input").addEventListener("blur", function () {
// alert("离开")
// })
//当表单元素离开并且内容发生改变时触发
document.getElementById("select").addEventListener("change", function () {
alert("改变")
})
//当表单元素获取焦点时触发
document.getElementById("btn").addEventListener("click", function () {
// document.getElementById("input").focus();
document.getElementById("input").select();
})
</script>
</body>
</html>
将鼠标移到面板上,数值发生变化
4.对象
代码如下(示例):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// let obj = {
// age: 18,
// name: "dawson",
// say: function () {
// console.log("说话");
// }
// };
// console.log(obj.age);
// console.log(obj);
// console.log(obj.say());
// let Student = function (age, name) {
// this.age = age;
// this.name = name;
// this.say = function () {
// console.log("我要说话");
// }
// }
// Student.prototype.sex = "男";
// Student.prototype.study = function () {
// console.log("我要学习");
// }
// let s = new Student(19, "福约翰")
// console.log(s);
class Person {
//构造函数
constructor(name, age) {
this.name = name;
this.age = age;
}
say() {
console.log("说话");
}
}
class Student extends Person {
constructor(name, age, sex, type) {
super(name, age);
this.sex = sex;
this.type = type;
}
study() {
console.log("我要学习");
}
}
let student = new Student("dawson", 18, "男", "大二")
console.log(student);
</script>
</body>
</html>
5.节点
代码如下(示例):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul id="box">
<li class="ulli">1</li>
<li class="ulli">2</li>
<li class="ulli">3</li>
</ul>
<script>
//节点的操作
//返回一个dom节点
// document.getElementById().style.color = "red"
// //返回一个类似数组的元素
// document.getElementsByClassName("ulli")
// //返回一个类似数组的元素
// document.querySelectorAll("ul>.ulli")
// //返回一个dom节点
// document.querySelector("ul>.ulli")
// let node = document.createElement("a")
// node.setAttribute("href", "http://www.baidu.com")
// node.innerText = "百度一下"
//将元素插入指定元素内的末尾
// document.body.append(node)
// document.getElementById("box").prepend(node)
// document.getElementById("box").after(node)
// document.getElementById("box").before(node)
console.log(document.getElementsByClassName("ulli")[0].parentElement.children[1].previousElementSibling);
console.log(document.getElementsByClassName("ulli")[0].parentElement.children[1].nextElementSibling);
</script>
</body>
</html>
6.字符串
代码如下(示例):
//字符串的创建形式
let str = new String("1234");
let str = "1234dgfdg";
//字符串属性
let str = "123123"
str.length
String.prototype.myAdd = function (_str) {
return parseFloat(this) + parseFloat(_str);
}
let a = str.myAdd("123123")
console.log(a);
//字符串的常用方法
let str = "abc,Der,G,,,,,";
//charAt()方法表示返回指定下标所对应的字符
str.charAt(1)
//indexOf()方法表示返回指定字符在当前字符串中首次出现的下标位置
//如果该字符串不存在 则返回-1
str.indexOf("1123")
//lastIndexOf()表示返回指定字符在当前字符串中最后一次出现的下标位置
str.lastIndexOf("1")
//字符串的截取 slice substr substring
str.substring(1,4)
//字符串的大小写转换
console.log(str.toLowerCase());
console.log(str.toUpperCase());
//字符串的拆分
console.log(str.split(""));
//字符串的替换
console.log(str.replace(",", ".").replace(",", "."));
while (str.indexOf(",") > -1) {
str.replace(",", ".")
}
//产生0-1的随机小数
Math.random()
//向上取整
Math.ceil(1.2)=2
//向下取整
Math.floor(1.9)=1
//四舍五入
Math.round(1.5)=2
let email = "fuyuehan.2022@163com"
function checkEmail(email) {
if (email.indexOf("@") <= 0 || email.indexOf(".") == -1) {
return false;
}
if (email.indexOf("@") > email.indexOf(".")) {
return false;
}
return true;
}
console.log(checkEmail(email));
let time = "2020-10-a9";
function checkTime(time) {
let arr = time.split("-");
if (arr.length != 3) {
return false;
}
if (isNaN(arr[0]) || isNaN(arr[1]) || isNaN(arr[2])) {
return false;
}
if (arr[0].length != 4 || arr[1].length != 2 || arr[2].length != 2) {
return false;
} return true;
}
console.log(checkTime(time));
function getKeyNum(str) {
let arr = str.split(""), num = {};
for (let n of arr) {
if (!num.hasOwnProperty(n)) {
num[n] = 1;
} else {
num[n] = num[n] + 1;
}
}
return num;
}
console.log(getKeyNum("1gfsjfgjhggjfgsdf"));
总结
提示:学习笔记
仅是个人观点,如有错误,请指出