setAttribute() 与 getAttribute() 用法剖析及选项卡操作的实例展示,这一篇就够了

Ⅰ、在 MDN 上 setAttribute() 与 getAttribute() 函数的用法解释(很详细):

1、Element.setAttribute():

其一、摘要:

设置指定元素上的某个属性值。如果属性已经存在,则更新该值;否则,使用指定的名称和值添加一个新的属性。
要获取某个属性当前的值,可使用 getAttribute();要删除某个属性,可使用 removeAttribute()

其二、语法:

element.setAttribute(name, value);

参数:
name 表示属性名称的字符串;
value 表示属性的值/新值;

如果指定的属性已经存在,则其值变为传递的值。如果不存在,则创建指定的属性。

其三、在下面的例子中,setAttribute() 被用于设置 <button> 上的属性;

HTML

<button>Hello World</button>

JavaScript

var b = document.querySelector(“button”);

b.setAttribute(“name”, “helloButton”);
b.setAttribute(“disabled”, “”);

代码说明:

A、上面对setAttribute()的第一次调用显示了将name属性的值更改为“ helloButton”
B、要设置布尔属性的值(例如禁用),可以指定任何值。 建议使用空字符串或属性名称。 重要的是,如果根本不存在该属性,则不管其实际值如何,都将其值视为真实。 该属性的缺失表示其值是false。 通过将Disabled属性的值设置为空字符串(“”),我们将disable设置为true,这将导致按钮被禁用。

2、Element.getAttribute():

其一、摘要:

getAttribute() 返回元素上一个指定的属性值。如果指定的属性不存在,则返回 null 或 “” (空字符串);

其二、语法

let attribute = element.getAttribute(attributeName);

· attribute 是一个包含 attributeName 属性值的字符串。

· attributeName 是你想要获取的属性值的属性名称。

其三、例子:

let div1 = document.getElementById(“div1”);
let align = div1.getAttribute(“align”);

alert(align);
// shows the value of align for the element with id=“div1”

Ⅱ、我对 setAttribute() 与 getAttribute() 用法的理解:

1、setAttribute()

setAttribute():可以给元素添加属性,也可以修改元素已有的属性;

2、getAttribute()
getAttribute() :可以获得自定义的属性,也可以获得标准的属性;
自定义属性指:本身 html 标签没有这个属性,而是自己添加的属性;
标准属性是指:JS 提供给我们的属性,而元素打点访问的是标准的属性;

Ⅲ、用 getAttribute() 实现选项卡的操作:

1、问题描述:
其一、用JavaScript + html + css 实现,选项卡操作;

其二、分析:
A、用 html + css 实现布局(盒子的布置,空间的分配等);
B、用 JavaScript 中的 DOM 操作,实现点击不同选项显示不同标题栏的功能;

2、实现过程如下:
其一、运行软件VSCode,亲测可实现;
其二、运行代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      font-family: '微软雅黑';
      font-size: 14px;
    }
    #container {
      width: 398px;
      margin: 100px auto;
    }
    #container a {
      display: block;
      width: 98px;
      height: 42px;
      line-height: 42px;
      text-align: center;
      float: left;
      border-top: solid 1px #ff4400;
      border-bottom: solid 1px #ff4400;
      border-left: solid 1px #ff4400;
      color: #333333;
      text-decoration: none;
    }
    #container a:hover {  /* 鼠标放在 a 的标签上,其文字将显示为:红色; */
      color: #ff4400;
    }
    .content {
      width: 355px;
      height: 140px;
      text-align: center;
      border-right: solid 1px #ff4400;
      border-bottom: solid 1px #ff4400;
      border-left: solid 1px #ff4400;
      padding: 30px 0 0 40px;
      display: none;
    }
    .clear {    
      clear: left;
    }
    #container a.on {  /* 对a标签中的类名为:on 的标签进行操作; */
      background: #ff4400;
      color: #fff;
    }
  </style>
</head>
<body>
  <div id="container"> <!-- 在 div 下的 a 标签中添加一个自定义属性, -->
    <a href="#" class="on" index='0' >充话费</a> <!-- 类为:on 的 a 标签; -->
    <a href="#" index='1' >充流量</a>
    <a href="#" index='2' >充固话</a>
    <a href="#" index='3' style="border-right: 1px solid #ff4400;">充宽带</a> <!-- style 操作目的:使得显示的盒子最右边有边框; -->

    <div class="clear"></div>

    <div class="content" style="display: block;"> <!-- style 操作目的:使第一个图片在初始状态时,能显示出来; -->
      <img src="images/1.png" width="355px"/> 
      <!-- 该位置存放的是:图片的地址 (即:里面加载的是图片信息) -->
    </div>

    <div class="content">
      <img src="images/2.png" width="355px" />
      <!-- 该位置存放的是:图片的地址 (即:里面加载的是图片信息) -->
    </div>

    <div class="content">
      <img src="images/3.png" width="355px" />
      <!-- 该位置存放的是:图片的地址 (即:里面加载的是图片信息) -->
    </div>

    <div class="content">
      <img src="images/4.png" width="355px" />
      <!-- 该位置存放的是:图片的地址 (即:里面加载的是图片信息) -->
    </div>
  </div>
  <script>
    var as = document.getElementsByTagName('a'); //通过 DOM 操作,拿到标签为 a 的所有的元素(是个集合);
    var divs = document.getElementsByClassName('content');//通过 DOM 操作,拿到类为 content 的所有的元素(是个集合);

    for(var i=0; i<as.length; i++) {

      as[i].onclick = function() { //给 as集合 绑定单击事件;
        for(var j=0; j<as.length; j++) {
          as[j].className = '';            //将 as集合 的所有元素的类名全部取消; (此时用的思想为:排他思想;)
          divs[j].style.display = 'none';  //将 divs集合 的所有元素全设置成不显示状态;
        }

        this.className = 'on';      //仅将被单击的元素的类名设置为:on,以便执行在 css 中的相关操作;
        
        divs[this.getAttribute('index')].style.display = 'block';
        // 此时是:使用 this.getAttribute('index') 方法来获得自定义的属性;
        // 而若代码使用:divs[this.index].style.display = 'block'; 
        // 会报错,因为:index 是自定义的属性而不是标准的属性,通过 this.index 是拿不到 index 值的;
      }
    }
  </script>
</body>
</html>

其三、结果展示:
A、默认的显示结果:
在这里插入图片描述

B、点击 ‘充流量’ 后的显示结果:
在这里插入图片描述

C、点击 ‘充固话’ 后的显示结果:
在这里插入图片描述

D、点击 ‘充宽带’ 后的显示结果:
在这里插入图片描述

其四、也可以在上面 jsdivs[this.getAttribute('index')].style.display = 'block'; 代码下添加语句:this.setAttribute('id','www'); ,会发现在as[i].onclick事件触发后,会在 a标签中添加 id='www' 的属性:

A、添加代码后,在四个按钮未点击前,控制台的页面显示为:
在这里插入图片描述
B、添加代码后,在四个按钮点击后,控制台的页面显示为(添加了id='www'):
在这里插入图片描述

Ⅳ、选项卡操作的另一种方法:

若对该选项卡操作实例有兴趣,还有一种实现方法:
用 JavaScript + HTML + CSS 实现选项卡操作,点击不同选项就显示出不同的标题栏(并实现其他要求操作)
地址:https://blog.csdn.net/weixin_43405300/article/details/117903286

Ⅴ、小结:

其一、哪里有不对或不合适的地方,还请大佬们多多指点和交流!
其二、有兴趣的话,可以多多关注这个专栏(Vue(Vue2+Vue3)面试必备专栏):https://blog.csdn.net/weixin_43405300/category_11525646.html?spm=1001.2014.3001.5482

### 回答1: setattribute和getattributeJavaScript中的两个方法,用于设置和获取HTML元素的属性。 setattribute方法可以用来设置HTML元素的属性,例如: ``` document.getElementById("myElement").setAttribute("class", "myClass"); ``` 这会将ID为“myElement”的元素的class属性设置为“myClass”。 getattribute方法可以用来获取HTML元素的属性,例如: ``` var myClass = document.getElementById("myElement").getAttribute("class"); ``` 这会将ID为“myElement”的元素的class属性的值存储在变量myClass中。 ### 回答2: setAttribute和getAttribute都是Javascript中常用的方法,主要用于操作和获取HTML元素的属性。 setAttribute方法可以用来动态地改变HTML元素的属性值,例如: document.getElementById("myImg").setAttribute("src", "newImg.jpg"); 这段代码会将id为“myImg”的元素的src属性修改为“newImg.jpg”。setAttribute方法有两个参数,第一个参数是要设置的属性名,第二个参数是要设置的属性值。 getAttribute方法则用于获取HTML元素的属性值,例如: var title = document.getElementById("myTitle").getAttribute("title"); 这段代码会获取id为“myTitle”的元素的title属性值,并将其赋值给变量“title”。getAttribute方法有一个参数,即要获取的属性名。 需要注意的是,setAttribute和getAttribute只能操作和获取HTML元素的标准属性,不能操作和获取行内样式的属性。如果要操作和获取行内样式的属性,需要使用style属性。 另外,需要注意的是,在HTML5规范中,可以直接通过对象.属性的方式来设置和获取HTML元素的标准属性,例如: document.getElementById("myImg").src = "newImg.jpg"; var title = document.getElementById("myTitle").title; 但是,这种方式只适用于HTML元素的标准属性,不能操作和获取非标准属性或自定义属性。因此,在一些较老的浏览器中,仍然需要使用setAttribute和getAttribute方法来操作和获取HTML元素的属性。 ### 回答3: setAttribute和getAttributeJavaScript中DOM(Document Object Model)的元素属性操作方法。setAttribute方法用于设置指定元素的属性值,而getAttribute方法用于获取指定元素的属性值。 setAttribute方法需要两个参数:属性名和属性值。例如,如果要将元素的class属性设置为“myclass”,则可以使用以下代码: element.setAttribute("class", "myclass"); 使用getAttribute方法来检索属性值。例如,如果想要获取元素的class属性的值,则可以使用以下代码: var classname = element.getAttribute("class"); 当使用setAttribute方法设置属性值时,它将覆盖任何现有的相同属性。因此,在设置属性值之前,最好先检查其是否已存在。 getAttribute方法返回属性值的字符串表示形式。如果属性不存在,则返回null。请注意,它不会返回数字对象或布尔值。 在JavaScript中,setAttribute和getAttribute方法不仅适用于HTML元素,也适用于XML文档中的元素。因此,在处理XML文档时,这些方法比较有用。 总之,setAttribute和getAttribute方法对于操作DOM元素的属性非常有用,可以帮助我们控制HTML和XML文档的表现和行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

狮子座的男孩

如果可以,请我喝杯咖啡吧!

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

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

打赏作者

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

抵扣说明:

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

余额充值