Home > Web Front-end > JS Tutorial > JavaScript tutorial: detailed explanation of how to update, insert, and modify dom node instance code

JavaScript tutorial: detailed explanation of how to update, insert, and modify dom node instance code

伊谢尔伦
Release: 2017-07-20 13:30:55
Original
1574 people have browsed it

Update

After getting a DOM node, we can update it.

You can directly modify the text of the node. There are two methods:

One is to modify the innerHTML attribute, this The method is very powerful. Not only can you modify the text content of a DOM node, you can also directly modify the subtree inside the DOM node through HTML fragments:


// 获取<p id="p-id">...</p>
var p = document.getElementById(&#39;p-id&#39;);
// 设置文本为abc:
p.innerHTML = &#39;ABC&#39;; // <p id="p-id">ABC</p>
// 设置HTML:
p.innerHTML = &#39;ABC <span style="color:red">RED</span> XYZ&#39;;
// <p>...</p>的内部结构已修改
Copy after login

Use## When #innerHTML, please pay attention to whether you need to write HTML. If the written string is obtained through the network, pay attention to character encoding to avoid XSS attacks. The second is to modify the
innerText or textContent attribute, so that the string can be automatically HTML-encoded to ensure that it cannot be set. Any HTML tag:


// 获取<p id="p-id">...</p>
var p = document.getElementById(&#39;p-id&#39;);
// 设置文本:
p.innerText = &#39;<script>alert("Hi")</script>&#39;;
// HTML被自动编码,无法设置一个<script>节点:
// <p id="p-id"><script>alert("Hi")</script></p>
Copy after login

The difference between the two is that when reading attributes,

innerText does not return hidden elements of text, while textContent returns all text. Also note that IE<9 does not support textContent. Modifying CSS is also a frequently required operation. The
style attribute of the DOM node corresponds to all CSS and can be obtained or set directly. Because CSS allows names like font-size, but it is not a valid property name in JavaScript, it needs to be rewritten in JavaScript as camel case naming fontSize


// 获取<p id="p-id">...</p>
var p = document.getElementById(&#39;p-id&#39;);
// 设置CSS:
p.style.color = &#39;#ff0000&#39;;
p.style.fontSize = &#39;20px&#39;;
p.style.paddingTop = &#39;2em&#39;;
Copy after login

insert

When we get a certain DOM node, we want How to insert a new DOM into this DOM node?

If this DOM node is empty, for example,

, then use innerHTML = '< span>child' can modify the content of the DOM node, which is equivalent to "inserting" a new DOM node. If the DOM node is not empty, you cannot do this, because
innerHTML will directly replace all the original child nodes. There are two ways to insert new nodes. One is to use
appendChild to add a child node to the last child node of the parent node. For example:


<!-- HTML结构 -->
<p id="js">JavaScript</p>
<p id="list">
  <p id="java">Java</p>
  <p id="python">Python</p>
  <p id="scheme">Scheme</p>
</p>
Copy after login

Add

JavaScript

To the last item of

:

##

var
  js = document.getElementById(&#39;js&#39;),
  list = document.getElementById(&#39;list&#39;);
list.appendChild(js);
Copy after login

Now, HTML The structure becomes like this:


<!-- HTML结构 -->
<p id="list">
  <p id="java">Java</p>
  <p id="python">Python</p>
  <p id="scheme">Scheme</p>
  <p id="js">JavaScript</p>
</p>
Copy after login

Because the js node we inserted already exists in the current document tree, this node will first be deleted from its original location. Then insert it into the new location.

More often we will create a new node from scratch and then insert it into the specified position:



var
  list = document.getElementById(&#39;list&#39;),
  haskell = document.createElement(&#39;p&#39;);
haskell.id = &#39;haskell&#39;;
haskell.innerText = &#39;Haskell&#39;;
list.appendChild(haskell);
Copy after login

In this way we dynamically add a new node Node:


<!-- HTML结构 -->
<p id="list">
  <p id="java">Java</p>
  <p id="python">Python</p>
  <p id="scheme">Scheme</p>
  <p id="haskell">Haskell</p>
</p>
Copy after login

Dynamicly create a node and then add it to the DOM tree, which can achieve many functions. For example, the following code dynamically creates a