【内连样式获取】

<html>

<head>

<style>

.div2{

background:red;

}

</style>

</head>

<body>

<divid="div1"class="div2">测试</div>

<script>

varodiv=document.getElementById('div1');//先获取到要获取样式的元素标签,也就是获取到div1

//console.log(getComputedStyle(odiv,null).background);getComputedStyle("元素","伪类")是获取到计算后的样式,第二个参数是伪类,如果没有直接使用null但是万恶的IE8及之前不支持所以需要用到下面的方法

//console.log(currentStyle.background)这个只有IE本身支持也是获取到计算后的样式

console(window.getComputedStyle?getComputedStyle(odiv,null).background:odiv.currentStyle);//跨浏览器兼容

</script>

</body>

</html>

【外链样式获取】

<html>

<head>

<linkrel="stylesheet"type="text/css"href="basic.css"/>//外链的样式表

</head>

<body>

<divid="div1"class="div2">测试</div>

<script>

varsheet=document.styleSheets[0]//获取到外链的样式表

varrule=sheet.cssRules[0]//获取到外链样式表中的第一个样式

console.log(rule.style.background)//red这样就可以获得了外链样式表中指定的样式了

</script>

</body>

</html>

【外链样式表】

.div2{

background:red;

}

以上这篇JavaScript获取css行间样式,内连样式和外链样式的简单方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。

热门推荐

1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
4 怎么训练萨摩耶狗狗
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
9 狗狗胆子小怎么训练
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下