工作生活

查找DOM节点的属性和样式

2019-07-01  本文已影响0人  好名字都让你们用了
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    .one{
        font-size: 15px;
        height: 20px;
        width: 150px;
        background: green;
    }
    </style>
</head>
<body>
 <div class="one" style="color: red">起个名字真费劲</div>
</body>
</html>
one.getAttribute("class")
可以得到字符串类型的class值"one"

②获取class的值.

one.classList(得到一个数组)
one.className(得到字符串one)

③dom.style.xxx

dom.style.color
dom.style.fontSize(仅限内联样式,font-size写成fontSize)

④获取外部样式的值

getComputedStyle(dom).color
注:ComputedStyle是所有样式叠加后的样式
上一篇 下一篇

猜你喜欢

热点阅读