js 控制标记样式
做一个变色的标签
鼠标移入变为灰色,移除变回原来的颜色,点击变成黑色,再点击变回,如果变成黑色不受移入移除影响。
- <body>
- <div class="bt1" id="id1" style="background-color:red"></div>
- <div class="bt1" id="id2" style="background-color:yellow"></div>
- <div class="bt1" id="id3" style="background-color:blue" ></div>
- <div class="bt1" id="id4" style="background-color:green"></div>
- <div class="bt1" id="id5" style="background-color:#FF00FF"></div>
- </body>
- </html>
- <script type="text/javascript">
- var a = document.getElementsByClassName('bt1');//抓取class
- for (var i = ; i < a.length; i++) { //枚举
- a[i].index = i; //给每个元素记上一个i值
- a[i].onmouseover = function () { // 鼠标移入事件
- if (this.style.backgroundColor != "black") {
- this.style.backgroundColor = "#8B7E66"
- }
- }
- a[i].onmouseout = function () { //a鼠标移除事件
- if(this.index == && this.style.backgroundColor != "black" )
- this.style.backgroundColor = "red";
- if (this.index == && this.style.backgroundColor != "black")
- this.style.backgroundColor = "yellow";
- if (this.index == && this.style.backgroundColor != "black")
- this.style.backgroundColor = "blue";
- if (this.index == && this.style.backgroundColor != "black")
- this.style.backgroundColor = "green";
- if (this.index == && this.style.backgroundColor != "black")
- this.style.backgroundColor = "#FF00FF";
- }
- a[i].onclick = function () { //点击事件
- if (this.style.backgroundColor == "black") {
- if (this.index == )
- this.style.backgroundColor = "red";
- if (this.index == )
- this.style.backgroundColor = "yellow";
- if (this.index == )
- this.style.backgroundColor = "blue";
- if (this.index == )
- this.style.backgroundColor = "green";
- if (this.index == )
- this.style.backgroundColor = "#FF00FF";
- }
- else if(this.style.backgroundColor != "black"){
- //this.style.backgroundColor = color(this.index);
- for (var j = ; j < a.length; j++) {
- if (a[j].index == )
- a[j].style.backgroundColor = "red";
- if (a[j].index == )
- a[j].style.backgroundColor = "yellow";
- if (a[j].index == )
- a[j].style.backgroundColor = "blue";
- if (a[j].index == )
- a[j].style.backgroundColor = "green";
- if (a[j].index == )
- a[j].style.backgroundColor = "#FF00FF";
- }
- this.style.backgroundColor = "black";
- }
- }
- }
- </script>
外联样式表
- .bt1 {
- float:left;
- width:100px;
- height:50px;
- }
js 控制标记样式的更多相关文章
- js控制div样式显示与隐藏,JS通过点击超链接右边(指定位置)显示一个图标
原文出自:https://blog.csdn.net/seesun2012 javascript基础篇,老土的方法解决js控制div样式,便于新手理解,粗暴的不能再粗暴,如果你是高手,请忽略! 思路: ...
- JS 控制CSS样式表
JS控制CSS所使用的方法: <style> .rule{ display: none; } </style> 你想要改变把他的display属性由none改为inline. ...
- JS——控制标记的样式
1.定义一个div,宽度为100px,高度为100px,背景色为粉色. 定义一个事件,鼠标移入时背景色变为蓝色,宽度变为200px. 定义一个事件,鼠标移出时背景色变为红色. html文件: < ...
- js控制style样式
1.行内样式获取打印出来 2.内嵌和外链的获取不了 <div style="width:200px;height:200px; background: red;">&l ...
- vue中通过js控制页面样式方法
在使用vue.js框架的时候,有时候会希望在页面渲染完成之后,再执行函数方法来处理初始化相关的操作,如果只处理页面位置.宽或者高时,必须要在页面完全渲染之后才可以,页面没有加载完成之前,获取到的宽高不 ...
- Vue.js 控制css样式
<script src="https://unpkg.com/vue/dist/vue.js"></script> <style type=" ...
- JS控制菜单样式切换
$('#subtabs a').each(function (i, ele) { var href = $(ele).attr("href"); if (location.href ...
- js控制不同的时间段显示不同的css样式
js控制不同的时间段显示不同的css样式 js函数,可以放到单独的js文件中也可以放到当前页的<head>标记之内 function getCSS(){ datetoday ...
- 内联样式自动出现,一般是js控制写入的
内联样式自动出现,一般是js控制写入的
随机推荐
- [软件工程基础]团队作业Week3
团队介绍 团队名:弗朗明哥舞步 团队角色 开发:杨艺媛,易子沐,赵晓宇,夏欣怡 测试:张华杰 PM:刘斯盾 刘斯盾 我是刘斯盾,喜欢编程,喜欢运动!很高兴和大家一起开发这个项目! 杨艺媛 我叫杨艺媛, ...
- Django 基础 视图系统
Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...
- hdoj-3791-二叉搜索树(二叉搜索树模板题)
#include <cstring> #include <cstdio> #include <iostream> using namespace std; type ...
- github提交代码时遇到”Everything up-to-date“问题的解决方式
需要创建一个新分支,将最新代码加入新分支, 再将新分支合并到主分支,然后提交主分支代码到github网站. ---------------------------------------------- ...
- 不带缓存IO和标准(带缓存)IO
linux对IO文件的操作分为: 不带缓存:open read.posix标准,在用户空间没有缓冲,在内核空间还是进行了缓存的.数据-----内核缓存区----磁盘 假设内核缓存区长度为100字节,你 ...
- stl_hash_map.h
stl_hash_map.h // Filename: stl_hash_map.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...
- Zeroc Ice 负载均衡之Icegrid simple
最近学习Icestorm的replicated例子,在本地计算机上面跑通了,但在两台机器上(一台服务器192.168.0.113,一台客户端192.168.0.188),怎么都跑不通.上网求助,大家给 ...
- CodeForces - 961D:Pair Of Lines (几何,问两条直线是否可以覆盖所有点)
You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordin ...
- Unity3D的SystemInfo类,用于获取运行设备硬件信息(CPU、显卡、类型等)
SystemInfo类中的静态变量: 中文显示: Rendering.CopyTextureSupport copyTextureSupport:(只读)支持多种复制纹理功能的情况. string ...
- FastAdmin 中的 formatter flag 分析
FastAdmin 中的 formatter flag 分析 效果 首先看看效果,这里的文字和颜色可以根据数据改变. 这是系统自带的分类管理. 代码 功能在 \public\assets\js\bac ...