Home  >  Article  >  Web Front-end  >  JS implementation example of method to access specified node of DOM object_javascript skills

JS implementation example of method to access specified node of DOM object_javascript skills

不言
不言Original
2018-04-04 17:19:191721browse

This article mainly introduces the method of JS to access the specified node of the DOM object, and analyzes the related operation skills of JavaScript for DOM element node attributes in the form of examples. Friends in need can refer to the following

The examples in this article describe JS implements the method of accessing the specified node of the DOM object. Share it with everyone for your reference, the details are as follows:

1 Introduction

Use the getElementById() method to access the specified id node, and use the nodeName attribute, nodeType attribute and nodeValue attribute to display the node name, node type and node value.

1, nodeNameAttribute

This attribute is used to obtain the name of a node.

[sName=]obj.nodeName

sName: String variable is used to store the name of the node.

2, nodeTypeAttribute

This attribute is used to obtain the type of a node.

[sType=]obj.nodeType

sType: String variable, used to store the type of node, the value of this type is numeric . The type of this parameter is shown in the table below.


Type Value Node name Description
Element ) 1 tag Any HTML or XML tag
Attribute 2 Attribute Attribute in tag
Text (text) 3 #text Contains the text in the tag
Comment 8 #comment HTML comment
Document 9 #document Document Object
Document Type (documentType) 10 DOCTYPE DTD Specification

3. nodeValueProperty

This property will return the value of the node.

[txt=]obj.nodeValue

txt: String variable is used to store the value of the node, except for text node types. The node values ​​of the type are all "null".

Second application

Access the specified node. In this example, the name of the specified node and the type of the node are displayed in the prompt box that pops up on the page. and the value of the node.

Three codes

<!DOCTYPE html>
<html>
<head>
<title>www.jb51.net 访问指定节点</title>
</head>
<body id="b1">
<h3 >三号标题</h3>
<b>加粗内容</b>
<script language="javascript">
 <!--
 var by=document.getElementById("b1");
 var str;
 str="节点名称:"+by.nodeName+"\n";
 str+="节点类型:"+by.nodeType+"\n";
 str+="节点值:"+by.nodeValue+"\n";
 alert(str);
 -->
</script>
</body>
</html>

The following prompt appears when running four Frame:

Related recommendations:

How to operate DOM elements in js

Detailed explanation of DOM event binding in js

Dom attribute Instructions

The above is the detailed content of JS implementation example of method to access specified node of DOM object_javascript skills. For more information, please follow other related articles on the PHP Chinese website!

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