Home >Web Front-end >JS Tutorial >A quick explanation of how JavaScript operates on element content

A quick explanation of how JavaScript operates on element content

藏色散人
藏色散人forward
2022-08-06 10:12:101896browse

This article will quickly explain how JavaScript operates elements. It is very simple. I hope it will be helpful to friends in need!

Rule: All obtained from the current element are strings

Principle: Giving a value means setting, not giving a value means getting

points For category 3:

1, operate the form element content

Set: form element.value=value;

Get: form element .value;

  <input type="text" id="text1" value=""> 
  <script>
    //   给值就是设置,不给值就是获取
    //   操作表单元素
      var text1=document.getElementById('text1');
    //   设置
      text1.value="123"
    //   获取
    console.log(text1.value);

2, operate ordinary closed tag content innerText/innerHTML

Setting: element.innerText=value;

Element.innerHTML= Value;

Get: element.innerText

Element.innerHTML

<div id="box">哈哈哈哈</div>
    <h2 id="box1">aaaaa </h2>
  <script>
    //   给值就是设置,不给值就是获取
     //操作普通闭合标签内容   innerText/innerHTML
     var box = document.getElementById('box');
    // 设置
    box.innerText="嘿嘿嘿";
    // 获取
    console.log(box.innerText);
 
    var box1 = document.getElementById('box1');
    box1.innerHTML='bbbbbbb';
    console.log(box1.innerHTML)
  </script>

3. The operating element is born with its own attributes

settings :Element.id=value;

Get:Element.id;

 <div id="box2">1111 </div>
  <script>
    //   给值就是设置,不给值就是获取
     // 操作元素天生自带属性
     var box2 = document.getElementById('box2');
    box2.id="box22"
    console.log(box2.id)
  </script>

Related recommendations: [JavaScript Video Tutorial]

The above is the detailed content of A quick explanation of how JavaScript operates on element content. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete