当前位置:网站首页>js之排他思想及案例
js之排他思想及案例
2022-04-23 06:25:00 【小白蛋挞】
案例一:五个按钮,点击谁谁的背景颜色变成粉色
var btns = document.getElementsByTagName('button');
for (var i = 0; i < btns.length; i++) {
btns[i].onclick = function() {
//先把所有的背景颜色去掉
for (var i = 0; i < btns.length; i++) {
btns[i].style.backgroundColor = '';
}
//然后再让当前的元素按钮变色
this.style.backgroundColor = 'pink';
}
}
案例二百度换肤效果
var imgs = document.querySelector('.baidu').querySelectorAll('img');
//循环注册事件
for (var i = 0; i < imgs.length; i++) {
imgs[i].onclick = function() {
//点击图片的路径给body
// console.log(this.src);
document.body.style.backgroundImage = 'url(' + this.src + ')'
}
}
案例三表格经过变色
//获得
var trs = document.querySelector('tbody').querySelectorAll('tr');
for (var i = 0; i < trs.length; i++) {
//鼠标经过
trs[i].onmouseover = function() {
this.className = 'bg';
}
//鼠标离开恢复 onmouseout
trs[i].onmouseout = function() {
this.className = '';
}
}
案例四:表单全选取消全选
//1.全选和取消全选
//获取元素
var j_cbAll = document.getElementById('j_cbAll');
var j_tbs = document.getElementById('j_tb').getElementsByTagName('input');
//绑定事件
j_cbAll.onclick = function() {
//可以得到当前复选框的选中状态
// console.log(this.checked);
for (var i = 0; i < j_tbs.length; i++) {
j_tbs[i].checked = this.checked;
//这里不用++‘’是因为this.checked被识别为一个关键字
}
}
//2.复选按钮影响上面的
for (var i = 0; i < j_tbs.length; i++) {
j_tbs[i].onclick = function() {
//控制全选按钮是否选中
var flag = true;
// console.log(this.checked);
for (var i = 0; i < j_tbs.length; i++) {
if (!j_tbs[i].checked) {
flag = false;
//只要有一个没被选择中就不用判断剩下的了
break;
}
}
j_cbAll.checked = flag;
}
}
版权声明
本文为[小白蛋挞]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_61547956/article/details/124214061
边栏推荐
猜你喜欢
h5本地存储数据sessionStorage、localStorage
SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
js之DOM事件
ABAP 从CDS VIEW 发布OData Service示例
Mysql 索引
SAP PI/PO rfc2Soap 发布rfc接口为ws示例
Design optimization of MySQL database
组合数求解与(扩展)卢卡斯定理
如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列
随机推荐
游戏辅助脚本开发之旅
Applet newline character \ nfailure problem resolution - Daily pit stepping
Solutions to common problems in visualization (VII) solutions to drawing scale setting
组合数求解与(扩展)卢卡斯定理
typescript字典的使用
SAP ECC连接SAP PI系统配置
10.更新操作
[牛客挑战赛47]C.条件 (bitset加速floyd)
SAP 导出Excel文件打开显示:“xxx“的文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源,否则请勿打开。是否仍要打开它?
12.约束
简单易懂的子集dp
1.查看数据库和表
SAP Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。
The difference and application of VR, AR and MR, as well as some implementation principles of AR technology
ABAP 实现发布RESTful服务供外部调用示例
4. Multi table query
On BFC (block formatting context)
Two threads print odd and even numbers interactively
3.排序语句
11. Table and library management