• 每天一个JavaScript实例-展示设置和获取CSS样式设置


    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>每天一个JavaScript实例-展示设置和获取CSS样式设置</title>
    <style>
    	#date{
    		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>
    

  • 相关阅读:
    Django项目总结:项目主页
    变量、常量和作用域
    mysql数据库连接
    多线程下的单例模式
    JVM笔记--类文件结构
    Java初始化与清理
    多线程设计模式-单线程执行模式
    Java语言实现冒泡排序算法
    继承父类并实现多个接口_hehe.base.10.3
    Servlet | Request 对象获取请求参数的通用方式
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6806714.html
Copyright © 2020-2023  润新知