js中解决如何获取css中的参数
2016-04-24 23:06
701 查看
第一种方法:
通过获取内部css的style值来获取:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jsObject.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function test4(eventobj){
if(eventobj.value=="黑色"){
var di=document.getElementById("div1");
di.style.backgroundColor="black";
}else if(eventobj.value=="红色"){
var di=document.getElementById("div1");
di.style.backgroundColor="red";
}
}
</script>
</head>
<body>
<div id="div1" style="width:300px;height:300px; background-color:red;position:float">
div1
</div>
<input type="button" value="黑色" onclick="test4(this)"/>
<input type="button" value="红色" onclick="test4(this)"/>
</body>
</html>
第二种:通过获取外部引入css的参数:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jsObject.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="./my1.css">
<script type="text/javascript">
function test4(eventobj){
//获取引入的第一个css的样式表
var ocssRules = document.styleSheets[0].rules; //注意ie中使用document.styleSheets[0].rules 火狐、opera使用:document.styleSheets[0].cssrules
//获取css样式表中第一个样式
var style2 =ocssRules[0];
if(eventobj.value=="黑色"){
style2.style.backgroundColor="black";
}else if(eventobj.value=="红色"){
style2.style.backgroundColor="red";
}
}
</script>
</head>
<body>
<div class="style1">
div1
</div>
<input type="button" value="黑色" onclick="test4(this)"/>
<input type="button" value="红色" onclick="test4(this)"/>
</body>
</html>
通过获取内部css的style值来获取:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jsObject.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function test4(eventobj){
if(eventobj.value=="黑色"){
var di=document.getElementById("div1");
di.style.backgroundColor="black";
}else if(eventobj.value=="红色"){
var di=document.getElementById("div1");
di.style.backgroundColor="red";
}
}
</script>
</head>
<body>
<div id="div1" style="width:300px;height:300px; background-color:red;position:float">
div1
</div>
<input type="button" value="黑色" onclick="test4(this)"/>
<input type="button" value="红色" onclick="test4(this)"/>
</body>
</html>
第二种:通过获取外部引入css的参数:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jsObject.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="./my1.css">
<script type="text/javascript">
function test4(eventobj){
//获取引入的第一个css的样式表
var ocssRules = document.styleSheets[0].rules; //注意ie中使用document.styleSheets[0].rules 火狐、opera使用:document.styleSheets[0].cssrules
//获取css样式表中第一个样式
var style2 =ocssRules[0];
if(eventobj.value=="黑色"){
style2.style.backgroundColor="black";
}else if(eventobj.value=="红色"){
style2.style.backgroundColor="red";
}
}
</script>
</head>
<body>
<div class="style1">
div1
</div>
<input type="button" value="黑色" onclick="test4(this)"/>
<input type="button" value="红色" onclick="test4(this)"/>
</body>
</html>
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- Web布局连载——两栏固定布局(五)
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- [div+css]晒晒最新制作专题推广页模板
- 最后一次说说闭包
- Ajax
- 盒子模型
- CSS选择器
- CSS样式优先权
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- [css] line-height 百分比单位和数值单位的区别
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)