API自定义属性操作

一、获取属性值

        1.element.属性 获取属性值

        2.element.getAttribute('属性');

        区别:

        1.element.属性 获取内置属性值(元素本身自带的属性)

        2.element.getAttribute('属性')

                主要获得自定义的属性(标准)我们程序员自定义的属性

 <div id="demo" index="1" class="nav"></div>
    <script>
        var div = document.querySelector('div');
        // 1. 获取元素的属性值
        // (1) element.属性
        console.log(div.id);
        //(2) element.getAttribute('属性')  get得到获取 attribute 属性的意思 我们程序员自己添加的属性我们称为自定义属性 index
        console.log(div.getAttribute('id'));
        console.log(div.getAttribute('index'));
	</script>

 二、设置属性

        1.element.属性= '值' 设置内置属性值。

        2.element.setAttribute('属性','值')

        区别:

        element.属性  设置内置属性值

        element.setAttribute('属性')    主要设置自定义的属性

 // 2. 设置元素属性值
 // (1) element.属性= '值'
 div.id = 'test';
 div.className = 'navs';
 // (2) element.setAttribute('属性', '值');  主要针对于自定义属性
 div.setAttribute('index', 2);
 div.setAttribute('class', 'footer'); // class 特殊  这里面写的就是

 移出属性

element.removeAttribute('属性')

// class 不是className
// 3 移除属性 removeAttribute(属性)    
div.removeAttribute('index');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值