7-1实现输入input框的数据以li的形式加到页面中
indexOf 获取数组的下标
<script>
var arr=["html","css"];
//indexOf 获取数组的下标
console.log(arr.indexOf("html"));
</script>
实现
<input type="text" name="" id="input">
<ul id="app"></ul>
<script>
var arr = [];
var input = document.getElementById("input");
var app = document.getElementById("app");
input.onkeydown = function (event) {
if (event.keyCode == 13) {
if (arr.indexOf(this.value) == -1 && this.value!="") {
arr.push(this.value);
var li = document.createElement("li");
li.innerHTML = this.value;
app.append(li);
}
console.log(arr)
}
}
</script>
7-2实现从一个数组中获取想要的任意元素
<script>
var obj=[
{name:"ming",age:18,skill:"js"},
{name:"chen",age:18,skill:"vue"},
{name:"zheng",age:18,skill:"jquery"},
]
var arr=[];
for(var i=0;i<obj.length;i++){
// var name=obj[i].name;
// var skill=obj[i].skill;
var{name,skill}=obj[i];//解构
//键和值(key:value)相同的时候可以只写一个
arr.push({
name,//name:name,
skill//skill:skill
})
}
console.log(arr)
</script>
7-3实现界面切换
<style>
.parent {
position: relative;
}
.parent>div{
width: 200px;
height: 200px;
position:absolute;
}
.html{
background-color: red;
z-index: 100;
}
.css{
background-color: yellow;
}
.current{
color:blue;
}
</style>
</head>
<body>
<ul>
<li class="current">html</li>
<li>css</li>
</ul>
<div class="parent">
<div class="html">
html
</div>
<div class="css">
css
</div>
</div>
<script>
var lis = document.getElementsByTagName("li");
var divs=document.querySelectorAll(".parent div")
for (let i = 0; i < lis.length; i++) {
lis[i].index = i;//获取元素下标
lis[i].onclick = function () {
//1.遍历让所有的li移除class="current"
for (let i = 0; i < lis.length; i++) {
lis[i].classList.remove("current");
}
//2.让当前的元素添加class="current"
this.classList.add("current");
console.log(this.index)
//3让所有的div隐藏
for(let i=0;i<divs.length;i++){
divs[i].style.display="none"
}
//4.让对应的下标的div显示
divs[this.index].style.display="block";
}
}
</script>