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

藏色散人
Release: 2022-08-06 10:12:24
forward
1898 people have browsed it

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);
Copy after login

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>
Copy after login

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>
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template