底部
- header的top设置为0的时候就能看到
- btn的display设置为block的时候就能看到
- header的top设置为-200px的时候就看不到
- btn的display设置为none的时候就能看到

<div id="header">顶部div>
<button id="btn">回到顶部button>
// 0. 获取标签对象
var oDiv = document.querySelector('div');
var oBtn = document.querySelector('button');
// 1. 监听页面的滚动距离 从而决定页面的header和bun是否展示
window.onscroll = function(){
// console.log(document.documentElement.scrollTop);
// 当页面大于300的时候让herder和btn展示
if( document.documentElement.scrollTop >= 300 ){
// 此时让header和btn展示
oDiv.style.top = 0
oBtn.style.display = 'block';
}else{
// 否则不让header和btn展示
oDiv.style.top = -200 + 'px';
oBtn.style.display = 'none';
}
}
// 3. 回到顶部:监听but按钮的点击事件,点击时,让页面滚回到 scrollTop === 0
oBtn.addEventListener('click', function(){
// console.log(1111);
document.documentElement.scrollTop = 0;
})
- 如果type==password —> 那就给他赋值text
- 如果type == text —> 那就给他赋值password
密码: <input type="password" id="inp">
<button id="btn">这是一个眼睛图标button>
// 获取标签对象
var oPwd = document.querySelector('input');
var oBut = document.querySelector('button');
// 点击事件
oBut.addEventListener('click', function () {
/* 方法一:比较麻烦 */
// if(oPwd.getAttribute('type') === 'password'){
// oPwd.setAttribute('type', 'text');
// }else if(oPwd.getAttribute('type') === 'text'){
// oPwd.setAttribute('type', 'password');
// }
/* 方法二 */
// console.dir(inp.type);
if (oPwd.type === 'password') {
oPwd.type = 'text';
} else if (oPwd.type === 'text') {
oPwd.type = 'password';
}
})
- 点击全选按钮
- 判断我当前全选按钮的选中状态 ----> checked
- 根据全选按钮的选中状态, 决定是否让 1~4这几个按钮被选中
- 点击选项1~4其中一个按钮
- 判断当前选中1~4的选中状态是否都为选中
- 如果都是选中的, 将全选按钮选中
<input type="checkbox" name="" id="" class="All">全选
<hr>
<input type="checkbox" name="" id="" class="other">选项卡1
<input type="checkbox" name="" id="" class="other">选项卡2
<input type="checkbox" name="" id="" class="other">选项卡3
<input type="checkbox" name="" id="" class="other">选项卡4

代码的实现
oIptAll.addEventListener('click', function () {
// console.log(oIptAll.checked);
if( oIptAll.checked === true){
oIptOthers.forEach(function(item){
item.checked = true;
})
}else{
oIptOthers.forEach(function(item){
item.checked = false;
})
}
})
优化一
oIptAll.addEventListener('click', function () {
// console.log(oIptAll.checked);
if( oIptAll.checked === true){
oIptOthers.forEach(function(item){
item.checked = oIptAll.checked;
})
}else{
oIptOthers.forEach(function(item){
item.checked = oIptAll.checked;
})
}
})
优化二
oIptAll.addEventListener('click', function () {
// console.log(oIptAll.checked);
oIptOthers.forEach(function (item) {
item.checked = oIptAll.checked;
})
})
// oIptOther[0].addEventListener('click', function(){})
// oIptOther[2].addEventListener('click', function(){})
// oIptOther[3].addEventListener('click', function(){})
// oIptOther[4].addEventListener('click', function(){})
// oIptOther是伪数组 我们需要通过循坏获取每个值
oIptOther.forEach(function (item1) {
// 点击其中一个按钮时,判断其他几个的选中状态是否为true
// 如果都是true 那么选中全选 否则不选中全选
item1.addEventListener('click', function () {
oIptAll.checked = oIptOther.every(function (item2) {
return item2.checked;
})
})
})
// 获取元素
var oIptAll = document.querySelector('.All');
var oIptOther = [...document.querySelectorAll('.other')];
// 功能一
oIptAll.addEventListener('click', function () {
console.log(oIptAll.checked);
oIptOthers.forEach(function (item) {
item.checked = oIptAll.checked;
})
})
// 功能二
oIptOther.forEach(function (item1) {
// 点击其中一个按钮时,判断其他几个的选中状态是否为true
// 如果都是true 那么选中全选 否则不选中全选
item1.addEventListener('click', function () {
oIptAll.checked = oIptOther.every(function (item2) {
return item2.checked;
})
})
})
点击其中一个li的时候, 给点击的li添加一个类名, 然后给其他的li取消类名
根据选中哪一个header下的 li, 展示自身下的哪一个li

<ul class="header">
<li>header_1li>
<li class="active">header_2li>
<li>header_3li>
ul>
<ol class="content">
<li>content_1li>
<li class="active">content_2li>
<li>content_3li>
ol>
oUl.forEach(function(item1, index1){
item1.addEventListener('click' , function(){
oUl.forEach(function(item2, index2){
// 把所有的li取消类名
item2.classList.remove('active');
oOl[index2].classList.remove('active');
})
// 给自己添加类名
item1.classList.add('active');
oOl[index1].classList.add('active');
})
})
- 接收到后端给到的数据
- 将数据渲染到页面

<table>
<thead>
<tr>
<th>学号th>
<th>姓名th>
<th>性别th>
<th>年龄th>
<th>班级th>
tr>
thead>
<tbody>tbody>
table>
var users = [
{ id: 1, name: 'Jack', age: 18, gender: '男', classRoom: '2XX' },
{ id: 2, name: 'Rose', age: 20, gender: '女', classRoom: '2XX' },
{ id: 3, name: 'Jerry', age: 22, gender: '男', classRoom: '2XX' },
{ id: 4, name: 'Tom', age: 24, gender: '男', classRoom: '2XX' }
]
var oTbody = document.querySelector('tbody');
var str = '';
users.forEach(function(item){
str += `
${item.id}
${item.name}
${item.age}
${item.gender}
${item.classRoom}
`;
})
oTbody.innerHTML = str;
var users = [
{ id: 1, name: 'Jack', age: 18, gender: '男', classRoom: '2XX' },
{ id: 2, name: 'Rose', age: 20, gender: '女', classRoom: '2XX' },
{ id: 3, name: 'Jerry', age: 22, gender: '男', classRoom: '2XX' },
{ id: 4, name: 'Tom', age: 24, gender: '男', classRoom: '2XX' }
]
var str = users.reduce(function(prev, item){
return prev + `
${item.id}
${item.name}
${item.age}
${item.gender}
${item.classRoom}
`
}, '')
oTbody.innerHTML = str;