JS标签属性(自定义)

标签属性

1、获取和修改标签属性

可读可写

.style 获取的是行内样式

通过.的方式这能获取合法的标签属性(就是不能获取系统自带的方法属性),需要通过特定的方法来获取

<body>
    <img src="" alt="哈哈" name="dawang" id="box"/>
</body>
<script>
    var aMg = document.getElementsTagName('img');
    console.log(aMg[0].alt);  //获取
    aMg[0].alt = '二哈';  //修改
</script>

2、自定义标签属性

​ 1、定义:oBox.setAttrbute("goudan","Fyn");

​ 2、获取:oBox.getAttrbute("dashui");

​ 3、删除:removeAttrbute("dashui");

<body>
     <img src="tuper\x1.jpg" alt="阳阳" GHH="二哈">
</body>
<script>
    var aMg = document.getElementsByTagName('img');
    console.log(aMg[0].alt);
    
    aMg[0].setAttribute('name','Fyn');//自定义属性
    console.log(aMg[0].getAttribute('name'));//获取自定义属性
    aMg[0].removeAttribute('GHH'); //删除指定属性
</script>

猜你喜欢

转载自blog.csdn.net/qq_41741971/article/details/89677493