更新時(shí)間:2021-11-30 10:20:33 來源:動力節(jié)點(diǎn) 瀏覽1738次
1.任何支持 style 特性的 HTML 元素在 JavaScript 中都對應(yīng)著有一個(gè) style 屬性,指向一個(gè) CSSStyleDeclaration 的一個(gè)實(shí)例對象,包含該元素的內(nèi)嵌style樣式(直接定義在HTML元素上的style)。
對于使用短線分割的CSS屬性,在JavaScript中轉(zhuǎn)為駝峰式。
幾個(gè)常見的屬性:
CSS屬性 | JavaScript屬性 |
background-image | style.backgroundImage |
color | style.color |
display | style.display |
font-family | style.fontFamily |
height | style.height |
width | style.width |
有一個(gè)CSS屬性--->float,不能直接轉(zhuǎn)換為JavaScript的屬性,因?yàn)?float 在Javascript中是保留字。在 IE9+,F(xiàn)irefox、Safari、Opera、Chrome 中是 cssFloat,在同時(shí)所有IE也支持 styleFloat 。
以上改變樣式,會直接自動更新元素的表現(xiàn)。在標(biāo)準(zhǔn)模式下所有度量值都必須指定一個(gè)度量單位,如果沒有設(shè)置會被忽略。
2.“DOM2級樣式”中為 style 對象新添加的屬性和方法
cssText | 返回或設(shè)置style的CSS代碼 |
testDiv.style.cssText = "width:25px; height: 100px;background-color:green"; console.log(testDiv.style.cssText); |
length | CSS屬性的數(shù)量 | console.log(testDiv.style.length); |
parentRule | 返回表示CSS信息的CSSRule對象 | |
getPropertyCSSValue(propertyName) | 返回包含給定屬性名的CSSValue對象 |
返回的對象包含連個(gè)屬性:cssText -->該屬性的的字符串值; cssValueType -->css類型,數(shù)字常量,0(繼承的值)、1(基本的值)、2(值列表)、3(自定義的值) |
getPropertyValue(propertyName) | 返回給定屬性的字符串值 | testDiv.style.getPropertyValue("background-color"); |
getPropertyPriority(propertyName) | 如果給定的屬性使用了“!important",返回important,否則返回空字符串 | |
item(index)/方括號語法[index] | 返回給定索引的CSS屬性名稱 | testDiv.style.item(1); testDiv.style[1]; |
removeProperty(propertyName) | 刪除給定的屬性 | |
setProperty(propertyaName,value,priority) | 設(shè)置屬性,及優(yōu)先級(“important”或空字符串) |
var testDiv = document.getElementById("test");
testDiv.style.backgroundColor = "red";
for(var i=0, len=testDiv.style.length;i<len;i++){ // IE 9+、Safari、Chrome、Firefox、Opera 9+
var prop = testDiv.style[i];
var value = testDiv.style.getPropertyValue(prop);
console.log(prop + ": " + value);
}
testDiv.style.cssText = "width:25px; height: 100px;background-color:green";
console.log(testDiv.style.cssText);
瀏覽器支持:IE9+、Firefox、Safari、Opera 9+、Chrome
3.計(jì)算的樣式,document.defaultView.getComputedStyle()
計(jì)算樣式都是只讀的,也包含瀏覽器默認(rèn)CSS值,而有些屬性各個(gè)瀏覽器默認(rèn)值也不同。
getComputedStyle(element,pseudo-element),element是要計(jì)算樣式的元素,pseudo-element是偽元素(":after"、“:before”),沒有偽元素也可以是null。返回的是一個(gè)CSSStyleDeclaration對象
<style>
#mydiv{
background-color: blue;
width: 100px;
height:200px;
}
</style>
<div id="mydiv" style="background-color: red; border: 1px solid black"></div>
var mydiv = document.getElementById("mydiv");
var computedStyle = document.defaultView ? document.defaultView.getComputedStyle(mydiv,null) : mydiv.currentStyle; // IE8- 不支持document.defaultView,所有IE都支持currentStyle
console.log(computedStyle.backgroundColor); // rgb(255, 0, 0) ,IE: red
console.log(computedStyle.width); // 100px
console.log(computedStyle.height); // 200px
console.log(computedStyle.border); //1px solid rgb(0, 0, 0) , IE9+:空字符串,IE8-:undefined
console.log(computedStyle.borderLeftWidth); // 1px
顏色的返回值在各個(gè)瀏覽器也不同,有的會轉(zhuǎn)化RGB格式。
border是一個(gè)綜合屬性,它包含四個(gè)邊的邊框?qū)挾取㈩伾㈩愋偷龋鱾€(gè)瀏覽器解析不一樣。所以 computedStyle.border 有的返回有的為空。
4.操作樣式表
DOM2提供了操作樣式表的接口,可以操作通過<link>包含的樣式表和在<style>中定義的樣式。
以上就是關(guān)于“JS設(shè)置樣式”的介紹,如果您想了解更相關(guān)知識,不妨來關(guān)注一下動力節(jié)點(diǎn)的JavaScript教程,里面的課程內(nèi)容更加豐富,適合沒有基礎(chǔ)的朋友學(xué)習(xí),希望對大家能夠有所幫助。
初級 202925
初級 203221
初級 202629
初級 203743