当前位置:网站首页>js-dom
js-dom
2022-06-13 02:06:00 【Python's path to becoming a God】
js-Dom
brief introduction
DOM:Document Object Model
node
HTML Everything in the document is a node
- The whole document is a document node
- Every HTML An element is an element node
- HTML The text within the element is a text node
- Every HTML Attributes are attribute nodes
- Annotations are annotation nodes
document object
Common properties
| name | say bright |
|---|---|
| referrer | Returns the ****URL |
| URL | Returns the ****URL |
document.referrer
document.URL
document Common methods
| name | say bright |
|---|---|
| getElementById() | Return to have specified id Reference to the first object of |
| getElementsByName() | Returns a collection of objects with the specified name |
| getElementsByTagName() | Returns a collection of objects with the specified label name |
| write() | Write text to document 、HTML Expression or JavaScript Code |
Properties of a node
| The attribute name | describe |
|---|---|
| parentNode | Return the parent of the node |
| childNodes | Returns the collection of child nodes ,childNodes[i] |
| firstChild | Returns the first child of the node , The most common use is to access the text node of the element |
| lastChild | Returns the last child of the node |
| nextSibling | Next node |
| previousSibling | Last node |
var obj=document.getElementById("news");
var str=obj.lastChild.firstChild.nextSibling.nextSibling.innerHTML;
var str=obj.lastChild.lastChild.previousSibling.previousSibling.innerHTML;
var str=obj.lastElementChild.firstElementChild.innerHTML||obj.lastChild.firstChild.innerHTML;
alert(str);
element attribute
| The attribute name | describe |
|---|---|
| firstElementChild | Returns the first child of the node , The most common use is to access the text node of the element |
| lastElementChild | Returns the last child of the node |
| nextElementSibling | Next node |
| previousElementSibling | Last node |
Node information
var nodes=document.getElementById("nodeList");
var type1=nodes.firstChild.nodeType;
var type2=nodes.firstChild.firstChild.nodeType;
var name1=nodes.firstChild.firstChild.nodeName;
var str=nodes.firstChild.firstChild.nodeValue;
var con="type1:"+type1+"<br/>type2:"+type2+"<br/>name1:"+name1+"<br/>str:"+str;
document.getElementById("nodeList").nextSibling.innerHTML=con;
Operation node
Manipulate the properties of the node
| name | describe |
|---|---|
| getAttribute(“ Property name ”) | Get attribute value |
| setAttribute(“ Property name ”,“ Property value ”) | Setting property values |
var ele=document.getElementsByName("book");
var img=document.getElementById("image");
if(ele[0].checked){
img.setAttribute("src","images/dog.jpg");
img.setAttribute("alt"," I survived with the dog ");
img.nextSibling.innerHTML=" I survived with the dog ";
}
else if(ele[1].checked){
img.setAttribute("src","images/mai.jpg");
img.setAttribute("alt"," What if the haze comes ");
img.nextSibling.innerHTML=" What if the haze comes ";
}
Create and insert nodes
| name | describe |
|---|---|
| createElement( tagName) | Create a tag named tagName New element node of |
| A.appendChild( B) | hold B Nodes are added to A End of node |
| insertBefore( A,B ) | hold A The node is inserted into B Before the node |
| cloneNode(deep) | Copy a specified node |
var ele=document.getElementsByName("book");
var bName=document.getElementsByTagName("div")[0];
if(ele[0].checked){
var img=document.createElement("img");
img.setAttribute("src","images/dog.jpg");
img.setAttribute("alt"," I survived with the dog ");
bName.appendChild(img);
}
else if(ele[1].checked){
var img=document.createElement("img");
img.setAttribute("src","images/mai.jpg");
img.setAttribute("alt"," What if the haze comes ");
img.setAttribute("onclick","copyNode()")
bName.appendChild(img);
Delete and replace nodes
| name | describe |
|---|---|
| removeChild( node) | Delete the specified node |
| replaceChild( newNode, oldNode) attribute attr | Replace the specified node with another node |
var delNode=document.getElementById("first");
delNode.parentNode.removeChild(delNode);
var oldNode=document.getElementById("second");
var newNode=document.createElement("img");
newNode.setAttribute("src","images/f03.jpg");
oldNode.parentNode.replaceChild(newNode,oldNode);
Operation node style
change style attribute
document.getElementById("titles").style.color="#ff0000";
document.getElementById("titles").style.fontSize="25px ";
change className attribute
document.getElementById("cart").className="cartOver";
document.getElementById("cartList").className="cartListOver";
Get the style of the element
document.getElementById("cartList").display
document.getElementById("cartList").currentStyle.display
边栏推荐
- The first cell of devaxpress CXGRID after inserting a row is in focus editing status
- Rsync transport exclusion directory
- Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)
- [sequence structure, branch structure, loop structure, continue statement, break statement, return statement] (learning Note 6 -- C language process control)
- [work notes] xr872 codec driver migration and application program example (with chip debugging method)
- What is the path field—— Competitive advertising
- Delphi7 compressed pictures (BMP, JPG, PNG)
- js获取元素
- 移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式
- Viewing the ambition of Xiaodu technology from intelligent giant screen TV v86
猜你喜欢
![[programming idea] communication interface of data transmission and decoupling design of communication protocol](/img/cd/896d1bcad556ffcbf1007bc984afeb.jpg)
[programming idea] communication interface of data transmission and decoupling design of communication protocol

3、 Upload fabric photos to SQL server and provide name to display fabric photos

Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report

华为设备配置虚拟专用网FRR

STM32F103 IIC OLED program migration complete engineering code

Huawei equipment configures private IP routing FRR

Combining strings and numbers using ssstream

Share three stories about CMDB

如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】

Decoding iFLYTEK open platform 2.0 is a fertile land for developers and a source of industrial innovation
随机推荐
[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys
The new wild prospect of JD instant retailing from the perspective of "hour shopping"
How does Google's audience work?
Record: how to solve the problem of "the system cannot find the specified path" in the picture message uploaded by transferto() of multipartfile class [valid through personal test]
记录:如何解决MultipartFile类的transferTo()上传图片报“系统找不到指定的路径“问题【亲测有效】
6、 Implementation of warehouse out management function
Get started quickly cmake
QT realizes mind mapping function (II)
Restrict cell input type and display format in CXGRID control
Simple ranging using Arduino and ultrasonic sensors
SQL Server 删除数据库所有表和所有存储过程
SQLserver2008 拒绝了对对象 '****' (数据库 '****',架构 'dbo')的 SELECT 权限
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO
Use of Arduino series pressure sensors and detected data displayed by OLED (detailed tutorial)
Parameter measurement method of brushless motor
Detailed explanation of maxpooling corresponding to conv1d, conv2d and conv3d machines of tensorflow2
SQL server deletes all tables and all stored procedures in the database
Day 1 of the 10 day smart lock project (understand the SCM stm32f401ret6 and C language foundation)
Viewing the ambition of Xiaodu technology from intelligent giant screen TV v86
如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】