100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > JS DOM获取标签/元素style样式

JS DOM获取标签/元素style样式

时间:2022-12-21 21:08:44

相关推荐

JS DOM获取标签/元素style样式

JS DOM获取标签/元素style样式

文章目录

JS DOM获取标签/元素style样式.stylegetComputedStyle()

.style

只能获取行内样式,不能获取style标签中class的样式

<body><div class="one" style="color:red;width:180px">小情歌</div><script>let one = document.querySelector(".one")console.log(one.style);</script></body>

<body><div class="one" style="color:red;width:180px">小情歌</div><script>let one = document.querySelector(".one")console.log(one.style.color);console.log(one.style.width);console.log(one.style.height);</script></body>

getComputedStyle()

获取所有设定样式,包括style标签中的class属性

<body><style>.one {height: 20px;}</style><div class="one" style="color:red;width:180px">小情歌</div><script>let one = document.querySelector(".one")console.log(getComputedStyle(one,null));</script></body>

<body><style>.one {height: 20px;}</style><div class="one" style="color:red;width:180px">小情歌</div><script>let one = document.querySelector(".one")console.log(getComputedStyle(one,null)["color"]);console.log(getComputedStyle(one,null)["color"]);console.log(getComputedStyle(one,null)["height"]);</script></body>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。