setAttribute(要获取的属性,属性值)
    获取元素属性

    1. <img src="" alt="">
    2. <script>
    3. var img = document.querySelector('img');
    4. img.setAttribute('src','./1.jpg')

    window.getComputedStyle
    可以获取计算过后的css属性值,使用style获取的是行间样式
    image.png
    currentStyle
    ie独有

    1. <head>
    2. <meta charset="UTF-8">
    3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    4. <title>Document</title>
    5. <style>
    6. .move{
    7. width: 100px;
    8. height: 100px;
    9. position: absolute;
    10. background: red;
    11. opacity: 0.5;
    12. }
    13. </style>
    14. </head>
    15. <body>
    16. <div class="move"></div>
    17. <script>
    18. var div = document.getElementsByClassName('move')[0];
    19. function getStyle(dom,attr){
    20. if(window.getComputedStyle){
    21. return window.getComputedStyle(dom,null)[attr];
    22. }else{
    23. return dom.currentStyle[attr];
    24. }
    25. }
    26. console.log(getStyle(div,'opacity'))
    27. </script>
    28. </body>