当前位置:网站首页>DOM (document object model)
DOM (document object model)
2022-06-24 18:35:00 【Brother Mengfan】
Catalog
4.2、 Get... According to the tag name
4.3、 according to name Property to get
4.4、 Get... According to the class name
1、DOM What is it?
2、DOM Trees

3、 node

4、 Get elements
4.1、 according to id obtain
<div id="box"> This is a block </div>
<script>
var box = document.getElementById('box');
console.log(box);
console.log(typeof box);
</script>4.2、 Get... According to the tag name
<ul>
<li> Beijing </li>
<li> tianjin </li>
<li> Shanghai </li>
<li> Chongqing </li>
</ul>
<script>
var lis = document.getElementsByTagName('li');
console.log(lis);
</script> <ul id="first">
<li> Beijing </li>
<li> tianjin </li>
<li> Shanghai </li>
<li> Chongqing </li>
</ul>
<script>
// Get the parent element
var ul = document.getElementById('first');
console.log(ul);
// Get child elements from parent elements
lis = ul.getElementsByTagName('li');
console.log(lis);
</script>4.3、 according to name Property to get
<p> <input type="checkbox" name="hobby" value=" music "> music
<input type="checkbox" name="hobby" value=" game "> game
</p>
<script>
var hobbies = document.getElementsByName('hobby');
console.log(hobbies);
</script>4.4、 Get... According to the class name
<div class="box">div1</div>
<div class="box">div2</div>
<script>
var divs = document.getElementsByClassName('box');
console.log(divs);
</script>4.5、 Get from selector
<div id="box1"> Yes id Of div</div>
<div class="box2"> Have a class style div</div>
<div class="box2"> Have a class style div</div>
<p> <input type="text" name="account" value="xian">
<input type="radio" name="gender" value=" male "> male
<input type="radio" name="gender" value=" Woman "> Woman </p>
<script>
// Get from tag
var div = document.querySelector('div');
console.log(div);
var divs = document.querySelectorAll('div');
console.log(divs);
// according to id obtain
div = document.querySelector('#box1');
console.log(div);
// According to the class style
divs = document.querySelectorAll('.box2');
console.log(divs);
// Get... According to the attribute selector
var gender = document.querySelectorAll('input[type="radio"]');
console.log(gender);
</script>4.6、 obtain body Elements
<div class="box"></div>
<input type="text" name="" id="">
<h1>h1</h1>
<script>
var body = document.body;
console.log(body);
</script>4.7、 obtain html Elements
<div class="box"></div>
<input type="text" name="" id="">
<h1>h1</h1>
<script>
var html = document.documentElement;
console.log(html);
</script>5、 Operational elements
5.1、 Change content

Example :
<div class="box"> This year is <strong>2022</strong> The first day of the year </div>
<script>
// 1. Get elements
var box = document.querySelector('.box');
console.log(box);
// 2. Get the content of the element
// 2.1 innerHTML
var content = box.innerHTML;
console.log(content);
// 2.2 innerText
var content = box.innerText;
console.log(content);
// 3. Set element content
// 3.1 innerHTML distinguish htm label , Keep spaces and line breaks at the same time
box.innerHTML = '<font color="red"> This is a red word </font>';
// 3.2 innerText Don't recognize html label , Do not leave spaces and line breaks
// box.innerText = '<font color="red"> This is a red word </font>';
</script>(1)innerHTML Property will put the html Execute after element parsing , and innerText Will not parse content Medium html Elements .
5.2、 Element attributes

The demo case :
<img src="1.jpeg">
<script>
var img = document.querySelector('img')
// obtain src attribute
console.log(img.src);
// modify src attribute
img.src = '2.jpeg';
// Add attribute
img.alt = ' picture ';
img.title = ' Tips ';
console.log(img);
</script>5.3、 Form Elements
stay Web In development , Forms are a common element , utilize DOM You can manipulate the attributes of the following form elements .

These properties all have read and write operations , adopt Element object . Property name To get the element attribute value , adopt Element object . Property name = value To set the element attribute value . and checked、selected and disabled The value of an element attribute is of boolean type .
The demo case :
<input type="text" name="name" value="123456"><br>
<input type="checkbox" name=""><br>
<select name="">
<option value="">-- Please select --</option>
<option value=" Beijing "> Beijing </option>
<option value=" Shanghai "> Shanghai </option>
<option value=" Chongqing "> Chongqing </option>
</select>
<script>
var input = document.querySelector('input')
// obtain type
console.log(input.type);
// Set up type
//input.type = 'password';
// obtain value
console.log(input.value);
// Set up value
input.value = 'jock'
// obtain disabled
console.log(input.disabled);
// false
// Set up disabled
input.disabled = true;
// Get elements
var ck = document.querySelector('[type="checkbox"]');
console.log(ck);
// Get the selected state
console.log(ck.checked); // false
// Set the selected state
ck.checked = true;
// Get the drop-down list
var sl = document.querySelector('select');
console.log(sl);
//console.log(sl[2]);
var op = sl[2];
console.log(op.selected);
// Settings are selected
op.selected = true;
</script>5.4、 Style elements

5.4.1、style
<style>
.box {
width: 150px;
height: 100px;
background-color: blueviolet;
}
</style>
<div class="box"></div>
<script>
// adopt JS To change div Width and background color
var box = document.querySelector('.box')
//console.log(box);
// adopt style The way
box.style.width = '300px';
box.style.backgroundColor = 'red';
</script>5.4.2、className attribute
Example :
<style>
.box {
width: 150px;
height: 100px;
background-color: blueviolet;
}
.bg {
width: 200px;
height: 200px;
background-color: cornflowerblue;
}
.ft {
color: white;
}
</style>
<div class="box"> This is the text </div>
<script>
// adopt JS To change div Width and background color
var box = document.querySelector('.box');
console.log(box.className);
box.className = 'bg ft'
</script>
explain :
(1) Through object .className To get the style name of the current object ;
(2) Through object .className=' value ' You can style the current object .
5.5、 Operation properties 、
5.5.1、setAtrribute()
This method can set attributes for elements . Its syntax is as follows :
Object name .setAttribute(' Property name ', ' Property value ');
<style>
div {
width: 100px;
height: 30px;
border: 1px solid #000000;
}
.bg {
background-color: cornflowerblue;
color: white;
}
</style>
<div title=" This is a title, "> This is the content </div>
<script>
var div = document.querySelector('div')
// Set properties
div.setAttribute('class', 'bg');
div.setAttribute('title', ' I changed the title ');
</script>5.5.2、getAttribute()
<div title=" This is a title, "> This is the content </div>
<script>
var div = document.querySelector('div')
// obtain title attribute
//console.log(div.title);
var attrValue = div.getAttribute('title');
console.log(attrValue);
</script>5.5.3 removeAttribute()
<div title=" This is a title, "> This is the content </div>
<script>
var div = document.querySelector('div');
// Delete title attribute
div.removeAttribute('title');
</script>5.5.4、 Custom properties

Example :
<div data-index="1" data-list-name="jock"></div>
<script>
var div = document.querySelector('div');
console.log(div.getAttribute('data-index'));
div.setAttribute('data-title', ' title ');
</script>6、DOM event
6.1、 What is an event
6.2、 Elements of the event
<button id="btn"> Click I will trigger the event </button>
<script>
// 1. Get elements
var btn = document.querySelector('#btn')
// 1. Event source : This button is the event source
// 2. Event type : This button will not trigger until it is clicked , Then its event type is click event
// 3. Event handler
btn.onclick = function() {
console.log(' You ordered me ');
};
</script>6.3、 Common mouse event types

Example :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Common mouse event types </title>
<style>
.box {
width: 150px;
height: 100px;
border: 1px solid #000000;
}
.bg {
background-color: cornflowerblue;
}
</style>
</head>
<body>
<div class="box"></div>
<input type="text" id="account" value="123456">
<script>
var box = document.querySelector('.box')
// Click events :onclick
/*box.onclick = function () {
console.log(' What do you want me to do ?');
};
// Double-click the event :ondblclick
box.ondblclick = function () {
console.log(' Stop bugging me. .');
};
*/
// Triggered when mouse passes :onmouseover
box.onmouseover = function () {
this.className = 'box bg';
};
// Triggered when the mouse leaves :onmouseout
box.onmouseout = function () {
this.className = 'box';
};
// Trigger when mouse moves :onmousemove
box.onmousemove = function () {
console.log(' Why do you touch me ');
};
var input = document.querySelector('#account')
// Triggered when the mouse focus is obtained :onfocus
input.onfocus = function () {
//console.log(this.value)
this.style.backgroundColor = 'cornflowerblue';
};
// Triggered when the mouse focus is lost :onblur
input.onblur = function () {
this.style.backgroundColor = '';
console.log(this.value);
};
// Triggered when the content of the text box changes :onchange
input.onchange = function () {
console.log(' The content has been modified ');
};
// Triggered when the content of the text box is selected :onselect
input.onselect = function () {
console.log(' I don't want to be a cadre ');
};
</script>
</body>
</html>边栏推荐
- 电子元器件行业B2B电商市场模式、交易能力数字化趋势分析
- Usage of typedef enum (enumeration)
- [JS Framework] Failed to execute the callback function:
- About whether arm's large and small end mode is related to CPU or compiler
- Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
- Recommend 14 commonly used test development tools
- [North Asia data recovery]_ mdb_ catalog. Mongodb database data recovery case in case of WT file corruption
- Keep two decimal places
- What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?
- Nine practical guidelines for improving responsive design testing
猜你喜欢
R language Quantitative Ecology redundancy analysis RDA analysis plant diversity species data visualization
Online sequence flow chart making tool

Network security database penetration of secondary vocational group in 2022

How to decompile APK files

Seven strategies for successfully integrating digital transformation

Wechat applet to realize stacked rotation

How does the chief information security officer discuss network security with the enterprise board of directors

C language - structure II

如何在 R 中使用 Fisher 的最小显着性差异 (LSD)

Flutter dart regular regexp matches non printing characters \cl\cj\cm\ck
随机推荐
How can programmers reduce bugs in development?
Uniapp wechat applet calls mobile map to navigate to the target point
How can an enterprise successfully complete cloud migration?
Application service access configuration parameters
[can you really use es] Introduction to es Basics (I)
腾讯云TCS:面向应用的一站式PaaS 平台
Three layer switching experiment
Business leaders compete for CIO roles
Selection (032) - what is the output of the following code?
Specification for self test requirements of program developers
TCE入围2020年工信部信创典型解决方案
variable
Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
130. surrounding area
Software testing methods: a short guide to quality assurance (QA) models
How MySQL works - Chapter 14
Keep two decimal places
Get max value of a bit column - get max value of a bit column
Redpacketframe and openmode packages
Redis series (3) - sentry highly available