原生js操作html熟悉,css样式等方法

1. obj.style:这个方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),而无法获取定义在<style type="text/css">里面的属性。而特殊的offersetWidth,offersetHeight是可以获取到定义在<style type="text/css">里面的宽高属性

<style type=”text/css”> 
#css88{
color:#cdcdcd;
widht:200px;
} 
</style> 
<body> 
<div id=”css88″ style=”height:200px; background:#333333″>JS获取CSS属性值</div> 
<script type=”text/javascript”> 
var div = document.getElementById(“css88″);
console.log(div.style.color);//为空 
console.log(div.style.width);//为空 
console.log(div.offsetWidth);//200 
console.log(div.style.height);//200px 
console.log(div.offsetHeight);//200 

</script> 
</body> 

 如果是一些标签 属性的值,可以通过getAttribute 或者直接访问属性去获取,但是要注意的是,自定义的一些属性无法通过直接访问属性去获取。

<body>
<img src="http://i" alt="" id="img" style="width: 100px" vid="10">
</body>
<script>
    var img = document.getElementById('img');
    console.log(img.getAttribute('src')) //http://i
    console.log(img.src) //http://i
    console.log(img.getAttribute('vid')) //10
    console.log(img.vid) //undefined
</script>

2. IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法 

“DOM2级样式”增强了document.defaultView,提供了getComputedStyle()方法。这个方法接受两个参数:要取得计算样式的元素和一个伪元素字符串(例如“:after”)。如果不需要伪元素信息,第二个参数可以是null。getComputerStyle()方法返回一个CSSStyleDeclaration对象,其中包含当前元素的所有计算的样式。以下面的HTML页面为例:

<html> 
<head> 
<title>计算元素样式</title> 
<style> 
#myDiv { 
    width:100px; 
    height:200px; 
} 
</style> 
<body> 
<div id ="myDiv" style=" border:1px solid black"></div> 

<script> 
var myDiv = document.getElementById("myDiv"); 
var computedStyle = window.getComputedStyle(myDiv, null); 

alert(computedStyle.backgroundColor); //"red" 
alert(computedStyle.width); //"100px" 
alert(computedStyle.height); //"200px" 
alert(computedStyle.border); //在某些浏览器中是“1px solid black” 

/*jq的源码中使用了document.defaultView.getComputedStyle(),与window.getComputedStyle()的有什么不同之处?*/
/*document.defaultView属性返回当前 document 对象所关联的 window 对象,如果没有,会返回 null。
IE9以下不支持。基本上用window.getComputedStyle()就可以了,只有一种情况必须用defaultView,
就是在firefox3.6上访问子框架内的样式 (iframe)*/
/*https://developer.mozilla.org/zh-CN/docs/Web/API/Document/defaultView*/

</script> 
</body> 
</head> 
</html></span>

边框属性可能也不会返回样式表中实际的border规则(Opera会返回,但其它浏览器不会)。存在这个差别的原因是不同浏览器解释综合属性的方式不同,因为设置这种属性实际上会涉及很多其他的属性。在设置border时,实际上是设置了四个边的边框宽度、颜色、样式属性。因此,即使computedStyle.border不会在所有浏览器中都返回值,但computedStyle.borderLeftWidth则会返回值。 

需要注意的是,即使有些浏览器支持这种功能,但表示值的方式可能会有所区别。例如,Firefox和Safari会返回将所有颜色转换成RGB格式。因此,即使getComputedStyle()方法时,最好多在几种浏览器中测试一下。 

还有获取到的是浏览器计算后的样式,如果你去获取background,你会得到下面结果,alert(a.background);//reb(255,0,0) none repeat sroll 0% 0% / auto padding-box border-box,所以请清楚指明你要获取的样式,像这样
alert(a.backgroundColor);//red


IE不支持getComputedStyle()方法,但它有一种类似的概念。在IE中,每个具有style属性的元素还有一个currentStyle属性。这个属性是CSSStyleDeclaration的实例,包含当前元素全部计算后的样式。取得这些样式的方法差不多,如下:

<span style="font-family:Arial;font-size:14px;">var myDiv = document.getElementById("myDiv"); 
var computedStyle = myDiv.currentStyle; 
alert(computedStyle.backgroundColor); //"red" 
alert(computedStyle.width); //"100px" 
alert(computedStyle.height); //"200px" 
alert(computedStyle.border); //undefined</span>

 

下面这个函数,能够获取一个元素的任意 CSS 属性值。

function getStyle(element, attr) {
        if(element.currentStyle) {
                return element.currentStyle[attr];
        } else {
                return getComputedStyle(element, false)[attr];
        }
}

比如,本例中如果想获得 lists 的 left 属性值,只需要

getStyle(lists,"left")

 

 

PS: js添加多个样式属性cssText

document.getElementById("box").style.cssText += ";color:red;font-size:20px";

代码分析:使用“+=”是为了防止之前的样式被清除,css代码前多一个分号是为了兼容万恶的IE。

              推荐使用jQuery中的$(selector).css({css_json_code}),而最优的方法是定义css类,直接添加类名去修改style。

              或者自己封装一个全局函数。例如如下代码,但是为避免浏览器重复渲染对性能产生影响,不推荐。

function setStyle(obj,json){
    for(var i in json)
    {
        obj.style[i]=json[i];
    }
}
setStyle(obj,{color : 'red', display : 'block'});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值