目录
一、操作节点
1.node.style:只能获取/修改行内的样式,不能够操作选择器里面的样式
2.getComputedStyle(node):获取元素当前的所有样式
<style>
.box {
width: 200px;
height: 200px;
background-color: pink
}
</style>
<div class="box">内容</div>
<script>
let box = document.querySelector('.box');
// 1.node.style 只能获取/修改行内的样式,不能够操作选择器里面的样式
box.style.color = 'red';
console.log(box.style.width); // 空字符串
// 2.getComputedStyle(node) 获取元素当前的所有样式
// console.log(getComputedStyle(box));
</script>
【案例】点击之后当前盒子变为宽高的一半
<style>
.box {
width: 200px;
height: 200px;
background-color: pink
}
</style>
<div class="box">内容</div>
<script>
let box = document.querySelector('.box');
// 点击之后当前盒子变为宽高的一半
box.onclick = function() {
// 给box设置行内的width 覆盖了选择器里面的width
box.style.width = parseFloat(getComputedStyle(box).width)*0.5 + 'px';
box.style.height = parseFloat(getComputedStyle(box).height)*0.5 + 'px';
}
</script>
【案例】高亮
<input type="text">
<button>搜索</button>
<div class="box">
省公安厅交通警察总队(省公安厅交通管理局)党委书记、
总队长(局长)张建芬涉嫌严重违纪违法,警察主动投案,
目前正接受省纪警察委监委纪律审查和监察调查。
</div>
<script>
var btn = document.querySelector('button');
btn.onclick = function() {
var keywords = document.querySelector('input').value;
var contents = document.querySelector('.box').innerText;
document.querySelector('.box').innerHTML = contents.replace(new RegExp(keywords, 'g'), `<span style = "color: red">${keywords}</span>`)
}
</script>
【案例】全选
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">
<input type="checkbox" name="selected" id="">
</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">
<input type="checkbox" name="selected" id="">
</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">
<input type="checkbox" name="selected" id="">
</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
<tr>
<th scope="row">
<input type="checkbox" name="selected" id="">
</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">
<input type="checkbox" name="selected" id="" value="12">
</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
<input type="text">
<input type="text">
<input type="text"> +
<input type="button" value="添加">
<hr>
<input type="button" value="全选" class="selectall">
<input type="button" value="不选" class="selectno">
<input type="button" value="反选" class="selectchange">
<script>
let selectall = document.querySelector('.selectall');
let selectno = document.querySelector('.selectno');
let selectchange = document.querySelector('.selectchange')
selectall.onclick = click;
selectno.onclick = click;
selectchange.onclick = click;
function click() {
console.log(1111);
let selected = document.getElementsByName('selected');
for (let i = 0; i < selected.length; i++) {
if (this.classList.contains('selectall')) {
selected[i].checked = true;
}
if (this.classList.contains('selectno')) {
selected[i].checked = false;
}
if (this.classList.contains('selectchange')) {
selected[i].checked = !selected[i].checked;
}
}
}
// 全选
// selectall.onclick = function() {
// for (let i = 0; i < selected.length; i++) {
// selected[i].checked = true;
// }
// }
// // 全不选
// selectno.onclick = function() {
// for (let i = 0; i < selected.length; i++) {
// selected[i].checked = false;
// }
// }
// // 反选
// selectchange.onclick = function() {
// // console.log(11);
// for (let i = 0; i < selected.length; i++) {
// selected[i].checked ? selected[i].checked = false : selected[i].checked = true; 方式一
// selected[i].checked = !selected[i].checked; // 方式二
// }
// }
</script>
二、操作class
操作class的方法 | 方法 | 功能 |
node.className | 修改、添加元素的类名 | |
node.classList | classList.add() | 在不影响原始class的情况下追加新的class |
classList.remove() | 删除指定的class,没指定的不会删除 | |
classList.contains() | 检查是否存在指定的class |
【案例】通过添加类改变背景颜色
<style>
.box {
width: 200px;
height: 200px;
background-color: green;
}
.active {
background-color: red;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
// 操作class
let box = document.querySelector('.box');
// 1.node.className == 'class'
box.onclick = function() {
if (this.className == 'box active') { // 类名必须带上box 不然盒子没有样式了
this.className = 'box';
} else {
this.className = 'box active';
}
}
/* 2.node.classList 类数组对象
classList.add():在不影响原始class的情况下追加新的class
classList.remove():删除指定的class,没指定的不会删除
classList.contains():检查是否存在指定的class
*/
box.onclick = function() {
if (!this.classList.contains('active')) {
this.classList.add('active');
} else {
this.classList.remove('active');
}
}
</script>
【案例】点击切换标签页
<style>
.outer div{
width: 200px;
height: 200px;
background-color: pink;
margin-top: 10px;
display: none;
}
.outer .text2 {
background-color: red;
}
.outer .text3 {
background-color: green;
}
.outer .text4 {
background-color: blue;
}
.outer > .text {
display: block;
}
.active {
background-color: black;
color: #fff;
}
</style>
</head>
<body>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<div class="outer">
<div class="text">文本1
</div>
<div class="text2">文本2</div>
<div class="text3">文本3</div>
<div class="text4">文本4</div>
</div>
<script>
let btn = document.querySelectorAll('button');
let divs = document.querySelectorAll('.outer div');
for (let i = 0; i < btn.length; i++) {
btn[i].onclick = function() {
for (let i = 0; i < divs.length; i++) {
divs[i].classList.remove('text');
btn[i].classList.remove('active');
}
divs[i].classList.add('text');
this.classList.add('active');
}
}
</script>
【案例】弹出模态框
<style>
* {
margin: 0;
padding: 0;
}
.button {
background-color: black;
color: #fff;
width: 50px;
height: 30px;
}
.outer {
width: 100vw;
height: 100vh;
background-color: black;
opacity: 0.4;
position: fixed;
top: 0;
left: 0;
display: none;
}
.box {
width: 300px;
height: 400px;
background-color: #fff;
position: absolute;
top: 50%;
left: 50%;
margin-top: -150px;
margin-left: -200px;
}
.close {
width: 30px;
height: 30px;
background-color: #ccc;
color: black;
position: absolute;
right: -10px;
top: -10px;
text-align: center;
line-height: 30px;
border-radius: 50px;
}
.close:hover {
cursor: pointer;
}
.block {
display: block;
}
</style>
</head>
<body>
<button class="button">登录</button>
<div class="outer">
<div class="box">
<div class="close">x</div>
</div>
</div>
<script>
let btn = document.querySelector('button');
let close = document.querySelector('.close');
let outer = document.querySelector('.outer');
// btn.onclick = function() {
// console.log(11);
// 1.点击登录按钮弹出模态框
// outer.classList.add('block');
// }
// 2.点击关闭按钮隐藏模态框
// close.onclick = function() {
// outer.classList.remove('block');
// }
// outer.onclick = function() {
// outer.classList.remove('block');
// }
btn.onclick = motai;
close.onclick = motai;
outer.onclick = motai;
function motai() {
if (this.classList.contains('button')) {
outer.classList.add('block');
}
if (this.classList.contains('close') || this.classList.contains('outer')) {
outer.classList.remove('block');
}
}
</script>
三、自定义属性
操作 | 方法 | 含义 |
获取元素属性值 | element.属性 | 获取元素固定的属性,如class、id,不能获取自定义属性 |
element.getAttribute('属性名') | 获取元素的自定义属性,也可用来获取固定属性 | |
element.dataset.属性 / [属性] | 只能获取data-开头的自定义属性! | |
设置元素属性值 | element.setAttribute('属性', '值') | 设置元素的自定义属性,通常以data-开头 |
<!-- index = "1" 为自定义属性 -->
<div id="demo" index = "1"></div>
<script>
let div = document.querySelector("div");
// 1.获取元素的属性值
// (1)获取元素固定的属性:element.属性
console.log(div.id); // demo
console.log(div.index); // undefined 不能用来获取自定义的属性
// (2)获取元素的自定义属性:element.getAttribute('属性名')
console.log(div.getAttribute('index')); // 1
console.log(div.id); // demo 也可以获取固定属性
// 2.设置元素属性值
// (1)设置元素固定的属性:element.属性 = '值'
div.id = 'test'; // <div id="test" index = "1"></div>
// (2)设置元素的自定义属性:element.setAttribute('属性', '值')
div.setAttribute('index1', '2') // <div id="demo" index = "1" index1 = "2"></div>
</script>
<div gettime = '43' data-index = "2" data-list-name = "andy"></div>
<script>
let div = document.querySelector("div");
// 1.为了区分自定义的属性和固定属性 通常以data-开头定义自定义属性
console.log(div.getAttribute('data-index')); // 2
div.setAttribute("data-time", "20"); // <div data-index = "2" data-time = "20"></div>
// 2.h5新增的获取自定义属性的方法 它只能获取data-开头的自定义属性!
console.log(div.dataset); // <div data-index = "2" data-list-name = "andy"></div>
console.log(div.dataset.index); // 2
console.log(div.dataset['index']); // 2
// 如果自定义属性里面有多个-链接的单词,我们获取的时候采取驼峰命名法
console.log(div.dataset.listName); // andy
console.log(div.dataset["listName"]); // andy
</script>
【案例】点击按钮切换图片
<style>
img {
width: 400px;
height: 300px;
}
</style>
<img src="./img/5.jpg" alt="">
<div data-src="./img/14.jpg">按钮1</div>
<div data-src="./img/17.jpg">按钮2</div>
<div data-src="./img/18.jpg">按钮3</div>
<div data-src="./img/19.jpg">按钮4</div>
<script>
//案例:点击按钮,切换图片
let btns = document.querySelectorAll("img~div");
for (let i = 0; i < btns.length; i++) {
btns[i].onclick = function () {
document.querySelector("img").src = this.dataset.src
}
}
</script>