join 数组拼接
可以将 数组里面的元素 拼接成 一个字符串
如果没有写 连接字符串 数组元素会以 逗号形式 拼接在一起
map
-
数组名.map(function(数组元素,下标))
可以遍历数组
对数组里面的元素进行 加工处理 返回值 新的数组
const arr = ['张飞', '关羽', '曹操']
arr.map(function (item) {
console.log(item + '老师');
})
location.href 跳转
获取完整的URL地址 对其赋值时用于地址的跳转
location.href = 'http://www.baidu.com'
locaction.search 获取网页网址栏 ? 后面内容
获取网页网址栏中 ? 后面部分
![](https://i-blog.csdnimg.cn/blog_migrate/9fa489d56f7353bf6fd92fe94f06f908.jpeg)
<body>
<!-- form 表单 如果没有指定 action 属性值 表示 表单里面的数据会提交到当前的页面 -->
<form action="" autocomplete="off">
用户名:<input type="text" name="user"> <br>
密 码:<input type="password" name="pwd"> <br>
<button class="submit">提交</button>
</form>
<button id="btn">获取查询字符串</button>
</body>
<script>
// 目标:获取 查询字符串
// 1. 获取 id = btn 这个按钮
// 2. 给 它注册点击事件
// 3. 使用 location.search 属性 获取 查询字符串 ?name属性值=value属性值&name属性值=value属性值
document.querySelector('#btn').addEventListener('click', function () {
console.log(location.search)
})
</script>
locaction.hash 获取网页 # 号 后面内容
当网页网址中出现 # 时 , 刷新网页就能获取 # 后面内容
location.reload( ) 刷新
用来刷新当前页面 传入 true 时 表示 强制刷新
<body>
<a href="#/my">我的1111</a>
<a href="#/friend">关注</a>
<a href="#/download">下载</a>
<button id="btn">刷新</button>
<script>
// 需求:获取到 hash 值 锚点
// location.hash
console.log(location.hash)
document.querySelector('#btn').addEventListener('click',
function () {
location.reload(true)
})
</script>
</body>
本地存储
-
可以将数据永久存储本地(用户的电脑) 除非手动删除 否则关闭页面也会存在
localStorage.setItem('属性名', '值') 存储数据
localStorage.getItem('属性') 获取数据
localStorage.removeItem('属性名') 删
localStorage.clear() 删除所有
存储数据
localStorage.setItem('属性名', '值')
localStorage.setItem('uname', '你好世界')
![](https://i-blog.csdnimg.cn/blog_migrate/fc9dac429218c2884f2c253272d581f4.jpeg)
获取数据
localStorage.getItem('属性')
console.log(localStorage.getItem('uname'));
![](https://i-blog.csdnimg.cn/blog_migrate/36585d841f4483d1494852ace6a597ad.jpeg)
删
localStorage.removeItem('属性名')
删除所有数据
localStorage.clear()
存储复杂数据类型
本地只能存储字符串,无法存储复杂数据类型.需要将复杂数据类型转换成JSON字符串,在存储到本地
JSON.stringify(变量名)
JSON.parse(需要转换的数据)
JSON.stringify(变量名) , 转换成字符串后
需要用 JSON.parse(需要转换的数据) 转换成 数组 或 对象
exec(要提取的字符串)
从一个字符好串中提取出 想要的内容 有 数组 没 null
const str = '开发,java,php,python,go'
// 需求:从 str 字符串中 提取 前端
// 1. 定义正则
const reg = /开发/
// 2. 使用 exec() 作用:从 一个字符串中 提取出 想要null
// 正则对象.exec(要提取的字符串)
const arr = reg.exec(str)
console.log(arr[0])
不加/^字符串/
括号里面有 我 就匹配
console.log(/我/.test('我很丑'))
加了 /^字符串/
前面第一个是 我 才匹配
console.log(/^我/.test('很丑我'))
/字符串$/
console.log(/我$/.test('我是够')) //不匹配
console.log(/我$/.test('是狗我')) // 匹配
/^字符串$/
必须数量和文字 一样 才匹配
console.log(/^哈哈$/.test('哈哈哈')) // 不匹配
console.log(/^哈哈$/.test('我哈哈哈')) // 不匹配
console.log(/^哈哈$/.test('哈哈')) // 匹配