<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>每天一个JavaScript实例-展示设置和获取CSS样式设置</title>
<style>
#date{
width:200px;
background-color:lime;
}
</style>
<script>
function getStyle(date,cssprop,cssprop2){
//IE
if(date.currentStyle){
return date.currentStyle[cssprop];
}else if(document.defaultView && document.defaultView.getComputedStyle){
//console.log(document.defaultView.getComputedStyle(date,null).getPropertyValue(cssprop));
return document.defaultView.getComputedStyle(date,null).getPropertyValue(cssprop2);
}else{
return null;
}
}
window.onload = function(){
var date = document.getElementById("date");
var color = getStyle(date,"background-color","background-color");
console.log(color);
date.style.width= "500px";
date.style.backgroundColor= "yellow";
console.log(date.style.width);
console.log(date.style.backgroundColor);
//数组表示法
date.style["fontFamily"] = "courier";
//展示覆盖属性
var style = date.getAttribute("style");
console.log(style); date.setAttribute("style","height:100px");
console.log(document.defaultView.getComputedStyle(date,null).getPropertyValue("fontFamily"));//null
console.log(document.defaultView.getComputedStyle(date,null).getPropertyValue("font-family"));//STHeiti var style = date.getAttribute("style"); //date.style["fontFamily"]="微软雅黑";
var font = getStyle(date,"fontFamily","font-family");
//console.log(font);
}
</script>
</head> <body> <div id = "date" style="color:purple">
aa
</div> </body>
</html>

每天一个JavaScript实例-展示设置和获取CSS样式设置的更多相关文章

  1. 使用jQuery设置和获取css样式

  2. js中获取css样式属性值

    关于js中style,currentStyle和getComputedStyle几个注意的地方 (1)用js的style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的.针对css ...

  3. 每天一个JavaScript实例-获取元素当前高度

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. 每天一个JavaScript实例-从一个div元素删除一个段落

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. 每天一个JavaScript实例-推断图片是否载入完毕

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. 每天一个JavaScript实例-动态省份选择城市

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. 每天一个JavaScript实例-递归实现反转数组字符串

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. 每天一个JavaScript实例-html5拖拽

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. 每天一个JavaScript实例-canvas绘图

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. (转)Python 操作 Windows 粘贴板

    转自: http://outofmemory.cn/code-snippet/3939/Python-operation-Windows-niantie-board Python 操作 Windows ...

  2. 查看gcc/g++默认include路径

    转自:http://gcc.gnu.org/ml/gcc-help/2007-09/msg00205.html `gcc -print-prog-name=cc1plus` -v `g++ -prin ...

  3. Selenium2+python自动化7-xpath定位【转载】

    前言 在上一篇简单的介绍了用工具查看目标元素的xpath地址,工具查看比较死板,不够灵活,有时候直接复制粘贴会定位不到.这个时候就需要自己手动的去写xpath了,这一篇详细讲解xpath的一些语法. ...

  4. bottle框架学习(2):变量定义等

    try: from simplejson import dumps as json_dumps, loads as json_lds except ImportError: # pragma: no ...

  5. CF985A Chess Placing【思维】

    [链接]:CF985A [题意]:给你n和n/2个数ai,每个ai和奇数.偶数比较距离(注意选了奇数,偶数的距离就不要算了,反之同理),求最小的答案. [代码]: #include <iostr ...

  6. Python的程序结构[4] -> 函数/Function[0] -> 函数与方法的区别

    函数与方法的区别 / Distinction of Function and Method 关于函数与方法的区别,可根据两者的定义看出, 函数function -- A series of state ...

  7. 第一讲work(axe)

    1,Dao package com.songyan.Dao; public interface Axe { public void chop(); } package com.songyan.Dao; ...

  8. 集合框架(高级for的使用)

    高级for循环 格式: for(数据类型 变量名 :被遍历的集合(collection)或者数组){} 对集合进行遍历,只能获取元素,但是不能对元素进行过多的操作 迭代器除了遍历,还可以进行remov ...

  9. Linux文本过滤常用命令(转)

    01 cat命令 通常用来显示文本文件的内容 一般用来查看比较短的文本文件,因为其缓冲区有限 -s选项可以用来合并文件中多余的空行,多个空行将被压缩为一个空行; -n选项可以显示行号 -b选项可以跳过 ...

  10. 检索COM 类工厂中CLSID 为{00024500-0000-0000-C000-000000000046}组件时失败

    检索 COM 类工厂中 CLSID 为{00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005 当在ASP.NET应用程序中引 ...