先看個(gè)例子
<div style=”font-size:100px;”>1111</div>
<p>2222</p>
<style>*{font-size:50px;}</style>
此時(shí) 如果用 document.querySelector("p").style.fontSize 是獲取不到50px 值的 而 document.querySelector("div").style.fontSize 返回的是100
因此可以得知document.querySelector(elements).style 只針對(duì)與標(biāo)簽上的屬性,如果在外部css 文件中的屬性如何獲取?
這里介紹兩個(gè)方法針對(duì)不同瀏覽器
1、 obj.currentStyle
2、window.getComputedStyle
function getCurCss(id,porp){
var obj = document.getElementById(id);
if (obj.currentStyle) {
return obj.currentStyle[prop];
} else if (window.getComputedStyle) {
propprop = prop.replace(/([A-Z])/g, “-$1”);
propprop = prop.toLowerCase();
return document.defaultView.getComputedStyle(obj, null)[prop];
}
return null;
}
getCurCss(id,”fontSize”);
新聞熱點(diǎn)
疑難解答
圖片精選