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控制写入的
随机推荐
- scrum meeting 1st
现状分析 这是一个新项目,在之前的阶段中,基本完成了用户需求分析,在具体实现方面,团队大部分处于初学阶段,需要时间学习试验,预计刚开始项目进展较慢,alpha阶段时间相对紧迫,打算先实现网站的基本功能 ...
- LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- Hibernate(2)
一.实体类编写规则 1.实体类里面的属性属于私有的 2.私有属性使用公开的set和get方法操作 3.要求实体类有属性作为唯一值(一般使用id值) 4.实体类属性建议不要使用基本数据类型,使用基本数据 ...
- hibernate.spring.xml
<!-- 配置数据库连接池(c3p0) --> <!-- <bean id="dataSource" class="com.mchange.v2. ...
- Codeforces Round #259(div2)C(数学期望)
数学题. 关键是求最大值为k时有多少种情况,结果是kn-(k-1)n-1.可以这么想:每一次都从1至k里选,共kn种,这里需要再减去每一次都从1至k-1里面选的情况.当然也可以分类计数法:按出现几次k ...
- 在Arcmap中加载互联网地图资源的4种方法(转载)
前一段时间想在Arcmap中打开互联网地图中的地图数据,如影像数据.基础地图数据等,经过简单研究目前总结了四种方法,整理下与大家分享,有些内容可能理解有误,希望大家多多指教.4种方法如下: a) ...
- 自动化框架httpClient实例
package com.auto.test.util; import java.net.SocketException;import java.net.SocketTimeoutException;i ...
- POJ3111(最大化平均值)
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 8458 Accepted: 2163 Case Time ...
- Java-API:java.util.Date
ylbtech-Java-API:java.util.Date Module java.base Package java.util Class Date java.lang.Object java. ...
- AngularJS:输入验证
ylbtech-AngularJS:输入验证 1.返回顶部 1. AngularJS 输入验证 AngularJS 表单和控件可以验证输入的数据. 输入验证 在前面的几个章节中,你已经学到关于 Ang ...