javascript对属性的操作

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>对属性的操作</title>
    </head>
    <body>
        <!--属性的设置-->
        <img src="img/lingzhiling.jpg" alt="" />
        <script type="text/javascript">
            //设置属性
            var img = document.getElementsByTagName('img')[0];
            img.setAttribute("alt", '加载失败的提示');
            //获取属性
            var imgAttr = img.getAttribute('alt');
            console.log(imgAttr);
            //添加属性
            img.setAttribute('height','500');
            img.setAttribute('title','这是一张图片');
            img.setAttribute('class','aaa');
            img.className = 'ccc';//这个是兼容问题的解决办法
        </script>
    </body>
</html>
原文地址:https://www.cnblogs.com/tanxiang6690/p/6860922.html