dom元素属性操作---属性获取或属性设置

一、属性的获取

  • 输出当前对象的类名称
<button class="btn list" name="but" id="data">按钮</button>
<script>
	var btn = document.getElementsByClassName("btn")[0];
   console.log(btn.className);
</script>

在这里插入图片描述

<button class="btn list" name="but" id="data">按钮</button>
<script>
	var btn = document.getElementsByName("but")[0];
   console.log(btn.name);
</script>

在这里插入图片描述

<button class="btn list" name="but" id="data">按钮</button>
<script>
	var btn = document.getElementsByName("but")[0];
   console.log(btn.id);
</script>

在这里插入图片描述

二、设置类属性

设置类属性

<button class="btn list" name="but" id="data" style="width: >100px;height: 30px;line-height: 30px;">按钮</button>
<script>
   var btn = document.getElementsByName("but")[0];
	btn.className = "info";
</script>

在这里插入图片描述

  • 直接设置类名称会覆盖掉原先的类名称。
  • 那么,怎么保留btn,把list覆盖掉呢?
<button class="btn list" name="but" id="data" style="width: 100px;height: 30px;line-height: 30px;">按钮</button>
<script>
     var btn = document.getElementsByName("but")[0];
	 btn.className = "btn info";
</script>

在这里插入图片描述

三、dom元素的class操作

1. 类添加的方法

btn.classList.add("data");

2. 返回元素的class列表

  • 返回DOMTokenList的class集合,value属性等价于className属性获取
console.log(btn.classList);

在这里插入图片描述

3. 检测元素是否具有某个类

  • 返回true或者false
  • btn.classList.contains();
console.log(btn.classList.contains("pin"));//false

4. 输出类总共有几个

console.log(btn.classList.length);//3

5. 移除类的方法

  • btn.classList.remove();
btn.classList.remove("data");

在这里插入图片描述

6. 类别切换

  • 要是有这个类,删除这个类。要是没有这个类,增加这个类
  • btn.classList.toggle();
  • btn.classList.toggle(“类名称”,boolean值);
  • boolean值为false值时,默认删除这个类名称,为true时,是添加。
<button class="btn list" name="but" id="data" style="width: >100px;height: 30px;line-height: 30px;">按钮</button>
<script>
	btn.classList.toggle("list");
</script>

在这里插入图片描述

<button class="btn list" name="but" id="data" style="width: >100px;height: 30px;line-height: 30px;">按钮</button>
<script>
	btn.classList.toggle("int");
</script>

在这里插入图片描述

<button class="btn list" name="but" id="data" style="width: >100px;height: 30px;line-height: 30px;">按钮</button>
<script>
	btn.classList.toggle("int",true);
</script>

在这里插入图片描述

<button class="btn list" name="but" id="data" style="width: >100px;height: 30px;line-height: 30px;">按钮</button>
<script>
	btn.classList.toggle("int",false);
</script>

在这里插入图片描述

四、元素的自定义属性的设置和获取

1. 如何设置自定义属性

(1)方法一

  • 设置的自定义属性不显示
<button class="btn" id="btn">按钮</button> 
<script>
    console.log(btn.id);//btn
    btn.id="but";
    but.timer=1000;
    console.log(but.timer);//1000
    but.index=1;
    console.log(but.index);//1 
</script>

(2)方法二

  • btn.setAttribute(“设置的自定义属性的名称”,“自定义的值”);
btn.setAttribute("cls-tag","自定义");

在这里插入图片描述

2. 获取自定义属性

  • btn.getAttribute(“自定义属性的名称”);
  • 返回的都是string类型
console.log(btn.getAttribute("cls-tag"));

在这里插入图片描述

3. 获取dom元素所有属性集合

console.log(btn.attributes);//返回值是对象 

在这里插入图片描述

  • 对象取值
  • 可以通过index(索引)获取,也可以通过key获取。
	console.log(btn.attributes['class']);//class="btn"
    console.log(btn.attributes[0]);//class="btn" 

4. 获取属性节点

   //获取属性节点
   console.log(btn.getAttributeNode('cls-tag'));//cls-tag="自定义"
   //获取属性节点值
   console.log(btn.getAttributeNode('cls-tag').value);//自定义
   //获取属性节点名称
   console.log(btn.getAttributeNode('cls-tag').name);//cls-tag

五、检测属性

1. hasAttribute()

  • 如果元素中存在指定的属性返回 true,否则返回false。
	console.log(btn.hasAttribute('cls-tag'));//true 

2. hasAttributes()

  • 如果元素有任何属性返回true,否则返回false。
	console.log(btn.hasAttributes());//true 

六、删除属性

1. removeAttribute()–从元素中删除指定的属性

  • 可见属性可以删除,不可见的属性删除无效
	btn.removeAttribute('cls-tag');
    console.log(btn.getAttribute('cls-tag'));//null
    btn.removeAttribute('index');
    console.log(btn.index);//1 

2. removeAttributeNode()–删除指定属性节点并返回移除后的节点。

var node=btn.getAttributeNode('cls-tag');
btn.removeAttributeNode(node);
console.log(btn.getAttributeNode('cls-tag'));//null
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南初️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值