一、字符串常用的方法
|
1
2
3
4
5
6
7
8
9
10
11
12
|
obj.length 长度
obj.trim() 移除前后空白
obj.trimLeft() 移除前空白
obj.trimRight() 移除后空白
obj.charAt(n) 返回字符串中的第n个字符
obj.concat(value, …) 拼接
obj.indexOf(substring,start) 子序列位置
obj.substring(from, to) 根据索引获取子序列
obj.slice(start, end) 切片
obj.toLowerCase() 大写
obj.toUpperCase() 小写
obj.split(delimiter, limit) 分割
|
二、数组常用的方法
1.obj.length 数组的大小
2.obj.push(ele) 尾部追加元素
3.obj.pop() 尾部获取一个元素
4.obj.unshift(ele)头部插入元素
5.obj.shift() 头部移出元素
6.obj.reverse() 反转
7.obj.join() 将数组元素链接起来构建一个字符串
8.obj.concat(val...) 连接数组
9.obj.sort() 对数组元素进行排序
三、属性方法
var s="hello world"; console.log(s.length); console.log(s.toUpperCase()); console.log(s.charAt(3));// 通过索引获取字符 console.log(s.indexOf("w"));// 通过字符获取索引 console.log(s.substr(1,3));// 字符串截断,substr(起始位置,截断长度) console.log(s.substring(1,3));// 字符串截断,substr(起始位置,截断位置) console.log(s.slice(1,3));// 字符串截断,substr(起始位置,截断位置)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.left{
width: 20%;
height: 500px;
float: left;
background-color: wheat;
}
.right{
float: left;
width: 80%;
height: 500px;
background-color: lightgray;
}
.title{
text-align: center;
line-height: 40px;
background-color: #0e90d2;
color: white;
}
.item{
padding: 10px;
}
.hide{
display: none;
}
</style>
</head>
<body>
<div class="outer">
<div class="left">
<div class="item">
<div class="title">菜单一</div>
<ul class="con">
<li>111</li>
<li>111</li>
<li>111</li>
</ul>
</div>
<div class="item">
<div class="title">菜单二</div>
<ul class="con hide">
<li>222</li>
<li>222</li>
<li>222</li>
</ul>
</div>
<div class="item">
<div class="title">菜单三</div>
<ul class="con hide">
<li>333</li>
<li>333</li>
<li>333</li>
</ul>
</div>
</div>
<div class="right"></div>
</div>
<script>
var eles_title=document.getElementsByClassName("title");
for (var i=0;i<eles_title.length;i++){
eles_title[i].onclick=function(){
this.nextElementSibling.classList.remove("hide");
for(var j=0;j<eles_title.length;j++){
if (eles_title[j]!=this){
eles_title[j].nextElementSibling.classList.add("hide")
}
}
}
}
</script>
</body>
</html>