search
HomeWeb Front-endJS TutorialAbout node content enhancement in javascript_Basic knowledge

1. Element node

Copy code The code is as follows:

//Test element node, output node name, node type, node value
var liElements=document.getElementsByTagName("li");
for(var i=0;i alert(liElements[i].nodeName);
alert(liElements[i].nodeType);
alert(liElements[i].nodeValue);
}

2. Attribute node

Copy code The code is as follows:

[/c//Test attribute node, output attribute node name, node type, node value
var liElements=document.getElementsByTagName("li");
for(var i=0;i var attrElement=liElements[i].getAttributeNode("value")
alert("attrElement.nodeName " attrElement.nodeName);
alert("attrElement.nodeType " attrElement. nodeType);
alert("attrElement.nodeValue " liElements[i].getAttribute("value"));
}ode]

3. Text node
[code]
//Test element node, output node name, node type, node value
var liElements=document.getElementsByTagName(" li");
for(var i=0;i alert(liElements[i].childNodes[0].nodeName);
alert(liElements[i] .childNodes[0].nodeType);
alert(liElements[i].childNodes[0].nodeValue);

liElements[i].childNodes[0].nodeValue="Nanjing";
alert(liElements[i].childNodes[0].nodeValue);

//Another way to read Method
alert(liElements[i].firstChild.nodeName);
alert(liElements[i].firstChild.nodeType);
alert(liElements[i].firstChild.nodeValue);
}

4. Replace node

replaceChild()
Replaces a child node in a given parent element with another child node
var reference = element.replaceChild(newChild,oldChild);
The return value is a pointer to the The reference pointer of the child node being replaced.
If the inserted child node also has child nodes, those child nodes are also inserted into the target node

Copy code The code is as follows:

//Method 1
// var cityElement= document.getElementById("city");
// var loveElement=document.getElementById("love");
// var cityChildElement=document.getElementById("beijing");
// var loveChildElement =document.getElementById("fankong");
// var oldElement=cityElement.replaceChild(loveChildElement,cityChildElement);
// loveElement.appendChild(oldElement);
// alert(oldElement.getAttribute( "id"));

var cityElement=document.getElementById("city");
var cityElement.onclick=function(){
var cityChildElement=document.getElementById("beijing");
var loveChildElement=document.getElement ById ("fankong");
var oldElement=cityElement.replaceChild(loveChildElement,cityChildElement);
loveElement.appendChild(oldElement);
alert(oldElement.getAttribute("id"));
}

5. Find attribute nodes

getAttribute()
Returns the value of a given attribute node for a given element
var attributeValue = element.getAttribute(attributeName);
The name of the given attribute must be passed in the form of a string Give the method.
The value of the given attribute will be returned in the form of a string. If the given attribute does not exist, getAttribute() will return an empty string.
Get the attribute node through the attribute
getAttributeNode (name of the attribute) --Node


  • Beijing


  • //Get the value of the attribute through the attribute name
    var bjElement=document. getElementById("bj");
    var attributeValue=eduElement.getAttribute("name");
    alert("attributeValue " attributeValue);

    //Get the node of the attribute through the attribute name
    var bjNode=eduElement.getAttributeNode("name");
    alert(eduNode.nodeValue);
    alert(eduNode.nodeType);
    alert(eduNode.nodeName);


    6. Set attribute nodes

    setAttribute()
    Adds a new attribute value to a given element node or changes the value of its existing attribute.
    element.setAttribute(attributeName,attributeValue);
    The name and value of the attribute must be passed to this method in the form of a string
    If this attribute already exists, its value will be refreshed;
    If If it does not exist, the setAttribute() method will first create it and then assign a value to it.


  • Beijing
  • //Get the reference of the element
    var bjElement=document.getElementById("bj");
    //Set the attribute value
    bjElement.setAttribute("name","beijing");
    //Get the set attribute value
    var nameValue=bjElement.getAttribute("name");
    alert("nameValue " nameValue);

    7. Create new element nodes

    createElement()
    Creates a new element node according to the given tag name. The method has only one parameter: the name of the element to be created, which is a string.
    var reference = document.createElement(element); The return value of the
    method: is a reference pointer pointing to the newly created node. The return value is an element node, so its nodeType attribute value is equal to 1.
    New element nodes will not be automatically added to the document. The new node has no nodeParent attribute. It is just an object that exists in the JavaScript context.
    var pElement = document.createElement("p");

    //Create a new element
    var pElement=document.createElement("li");
    //Set attribute value
    pElement.setAttribute("id","pid");

    //Get the parent element
    var loveElement=document.getElementById("love");
    //Add child elements to the parent element
    loveElement.appendChild(pElement);

    //Get the newly created element by id
    var pidElement=document.getElementById("pid");
    alert(pidElement.getAttribute("id"));

    8. Create a new text node

    createTextNode()
    Creates a new text node containing the given text. The return value of this method is a reference pointer to the new text node.
    var textNode = document.createTextNode(text); The
    method has only one parameter: the text string contained in the new text node. The return value of the
    method: is a reference pointer pointing to the new node. It is a text node, so its nodeType attribute is equal to 3.
    New element nodes will not be automatically added to the document, and new nodes have no nodeParent attribute

    var pElementText=document.createElement("li") ;
    var textElement=document.createTextNode("Nanjing");
    pElementText.appendChild(textElement);


    9. Insert node (1)

    appendChild()
    Add a child node to the given element:                                                                                                        the last child node of element . The return value of the
    method is a reference pointer to the newly added child node.
    This method is usually used in conjunction with createElement() createTextNode()
    New nodes can be appended to any element in the document

    Copy code The code is as follows:

    var newliElement=document.createElement("li");
    var textNode=document.createTextNode("Beijing");
    newliElement.appendChild(textNode);
    document.body. appendChild(newliElement);

    var liElement=document.getElementsByTagName("li");
    var textValue=liElement[0].firstChild.nodeValue;
    alert(textValue);

    10. Delete node

    removeChild()
    Removes a child node from a given element
    var reference = element.removeChild(node);
    The return value is a reference pointer to the deleted child node.
    When a node is deleted by the removeChild() method, all child nodes contained in this node will be deleted at the same time.

    Copy code The code is as follows:

      Beijing

    var ulElement=document.getElementById("city");
    var liElement=document.getElementById( "beijing");
    ulElement.removeChild(liElement);

    If you want to delete a node but don’t know which parent node it is, the parentNode attribute can help.

    Copy code The code is as follows:

      Beijing

    var liElement=document.getElementById("beijing");
    var parentElement=liElement.parentNode;
    parentElement.removeChild(liElement);

    11. Traverse the node tree

    ChildNodes: Returns an array consisting of the child nodes of a given element node:
    var nodeList = node.childNodes;
    Neither text nodes nor attribute nodes can contain any more child nodes, so they The ChildNodes property will always return an empty array.
    If you want to know whether an element has child nodes, you can use the hasChildNodes method.
    If you want to know how many child nodes a certain element has, you can use the length property of the childNodes array.
    The childNodes attribute is a read-only attribute.


    12. Get the first child node

    firstChild: This attribute returns the first child node of a given element node and returns the pointer of this node object.
    var reference = node.firstChild;
    Neither text nodes nor attribute nodes may contain any child nodes, so their firstChild property will always return null.
    The firstChild attribute of an element is equivalent to the first node in the childNodes node collection of this element, that is:
    var reference = node.ChildNodes[0];
    The firstChild attribute is a read-only attribute .


    13. Get the last child node

    lastChild: an attribute corresponding to firstChild.
    nextSibling: Returns the next sibling node of a given node.
    parentNode: Returns the parent node of a given node.
    The node returned by the parentNode attribute is always an element node, because only element nodes may contain child nodes.
    The document node has no parent node.
    previousSibling: Returns the previous sibling node of a given node


    14. innerHTML attribute

    Almost all browsers support this attribute, but it is not part of the DOM standard.
    The innerHTML attribute can be used to read and write the HTML content within a given element.

    Copy code The code is as follows:


    var divElement=document.getElementById("city");
    divElement.innerHTML="
  • Beijing
  • " ;
    Statement
    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
    Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

    Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

    JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

    JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

    C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

    C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

    From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

    JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

    Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

    Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

    The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

    C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

    JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

    JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

    JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

    The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software