<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
#box{
height: 200px;
width: 200px;
color: white;
background-color: orange;
}
</style>
</head>
<body>
<div style="text-align:center;" id="box">
<span style="font-size:20px;" id="txt">测试样例哈</span>
</div>
<script>
var box = document.getElementById("box");
var txt = document.getElementById("txt");
//1 .style.property 仅能获取内联样式 但可以修改样式
console.log(box.style.textAlign);//center
console.log(box.style.color);//空白
// box.style.color = "black";//√
//不可加!important //2 .style.cssText
console.log(box.style.cssText);//text-align:center, 返回
// txt.style.cssText = "color:red;";//注意,会覆盖之前的样式,包括在这里没有写到的,相当于清除了原本元素的样式
//但是继承而来的样式不会被清除,如这里继承自box的居中对齐
//可加!important,如用来覆盖继承的属性 //3 .style.setProperty
txt.style.setProperty("font-size","30px");//√
//仅可用于部分常用属性 //4 document.defaultView(window).getComputedStyle
//IE则是currentStyle[attr]
var declaration = document.defaultView.getComputedStyle(box,null);
var detailStyle = document.defaultView.getComputedStyle(box,null).height;
// var declaration = document.defaultView.getComputedStyle(box,null)[height];//同理
var windowDeclaration = window.getComputedStyle(box,null);
console.log(declaration,windowDeclaration,detailStyle); //4.sp 封装一个具有兼容性的属性查阅函数
function styleCheck (element,prop,state){
if(document.defaultView.getComputedStyle(element,state)){
result = document.defaultView.getComputedStyle(element,state)[prop];//不能.prop哈,记得
}else{
result = element.currentStyle.prop;
}
console.log("The value of property"+ prop + " is" + " " + result + ".");
return result;
}
styleCheck(box,"height"); //5. 对样式表下手 insertRule 与 addRule
// var changedStyleSheet = document.styleSheet[0].insertRule("#box","height","400px"); 插入新规则
</script>
</body>
</html>

最新文章

  1. 基于netty http协议栈的轻量级流程控制组件的实现
  2. Virtualbox 虚拟机支持硬件摄像头
  3. (原创)QuartusII设置虚拟引脚(Virtual Pin)
  4. DataTable筛选出现异常
  5. Eclipse问题集锦
  6. 11.APP打包成ipa文件,然后利用Application Loader 上架
  7. linux访问windows共享文件夹的方法
  8. ASP.NET的票据工具类FormsAuthenticationTicket
  9. Android njava.net.UnknownHostException: Unable to resolve host
  10. USB LPT 端口映射
  11. 关于java线程中stop interrupt daemon wait notify
  12. JavaBean实现用户登陆
  13. [ gczdac ] 20190213 开博客啦!
  14. redis的发布订阅、持久化存储、redis的主从复制
  15. 转://Oracle补丁及opatch工具介绍
  16. .net大型平台通过Nginx做负载均衡(Web层、中间服务层、DB层)
  17. 《剑指offer》第五十八题(左旋转字符串)
  18. schema的作用
  19. POJ-1953 World Cup Noise(线性动规)
  20. MongoDB3.x中添加用户和权限控制

热门文章

  1. 1091 N-自守数
  2. linux下的$0-n作用
  3. wc语法
  4. js文本转语音
  5. Oracle 导出的表不全,以及数据库版本不同导入报错
  6. rootfs 制作
  7. 本地代码上传到githup
  8. 关于Jordan标准形
  9. 【软件安装】nvidia驱动安装事宜
  10. nodeJS文件操作