获取内部样式或者外部样式的轮子

版权声明:互相学习,共同进步!! https://blog.csdn.net/hello_word2/article/details/84578117
/**
		obj:元素对象
		property:想要拿到的属性值
		*/
		function fetchComputedStyle(obj , property){

			//能力检测
			if(window.getComputedStyle){
				
				//现在要把用户输入的property中检测一下是不是驼峰,转为连字符写法
				//强制把用户输入的词儿里面的大写字母,变为小写字母加-
				//paddingLeft  →  padding-left
				property = property.replace(/([A-Z])/g , function(match,$1){
					
					return "-" + $1.toLowerCase();
				});

				return window.getComputedStyle(obj)[property];
			}else{
				//IE只认识驼峰,我们要防止用户输入短横,要把短横改为大写字母
				//padding-left  → paddingLeft 
				property = property.replace(/\-([a-z])/g , function(match,$1){
					return $1.toUpperCase();
				});

				return obj.currentStyle[property];
			}
		}

猜你喜欢

转载自blog.csdn.net/hello_word2/article/details/84578117