Home>Article>Web Front-end> Understand JavaScript WebAPI in one article

Understand JavaScript WebAPI in one article

WBOY
WBOY forward
2022-05-13 13:44:33 6723browse

This article brings you relevant knowledge aboutjavascript, which mainly introduces related issues about JavaScript WebAPI, including WebAPI background knowledge, obtaining elements, operating elements, operating nodes and some Let’s take a look at the case sharing below. I hope it will be helpful to everyone.

Understand JavaScript WebAPI in one article

[Related recommendations:javascript video tutorial,web front-end

1. WebAPI background knowledge

1.1 What is WebAPI

JS is divided into three major parts:

  • ECMAScript: Basic syntax part
  • DOM API: Manipulate the page structure
  • BOM API: Manipulate the browser

WebAPIincludesDOM BOM.

1.2 Basic concepts of DOM

What is DOM

DOMstands forDocument Object Model.

W3CThe standard provides us with a series of functions that allow us to operate:

  1. Webpage content
  2. Webpage structure
  3. Web page style

DOM tree

The structure of a page is a tree structure, calledDOM tree.
Understand JavaScript WebAPI in one article

Important concepts:

  • ##Document: A page is adocument, represented bydocument.
  • Element: All tags in the page are calledelements. Useelementto represent.
  • Node: All content in the web page can be callednode(label node, comment node, text node, attribute node, etc.). Usenodeto represent.
2. Get elements

2.1 querySelector

Using

querySelectorcan completely reuse the CSS selector knowledge learned previously to achieve a faster and more accurate way to obtain Element object

Syntax format:

let element = document.querySelector(selectirs);
  • selectorsFill in one or more selectors

Usage example:

abc

def

Run screenshot
Understand JavaScript WebAPI in one article##2.2 querySelectorAll

If you need to select with the specified A list of all elements matched by the filter, you should use

querySelectorAll()

Usage example:

123

456

Run screenshot


Understand JavaScript WebAPI in one article3. Manipulate elements

3.1 Get/modify element content

1. innerText

Element.innerText

Attribute represents the "rendered" text content of a node and its descendants

Note:

Does not recognize html tags. Is non-standard (initiated by IE). Read The result does not retain line breaks and spaces in the html source code.

Usage example:

hello world

hello world

Run screenshot

:Passed

innerText

Unable to getto thepinternalhtmlstructure, only the text content can be obtained.
Understand JavaScript WebAPI in one article2. innerHTML

Element.innerHTML

The property sets or gets the descendants of the element represented by HTML syntaxNote
:

    Identification
  • html tag. W3C standard. The read result retains line breaks and spaces in the html source code
Code example:

hello world

hello world

Run screenshot:

innerHTML

Not only can you get thehtmlstructure of the page, but you can also modify the structure. And the content you get is retained. Spaces and line breaks
Understand JavaScript WebAPI in one article3.2 Get/modify element attributes

Note:

Get attributes# throughelement.properties##Code example:

Understand JavaScript WebAPI in one article 
Run result:

##3.3 Get/modify form element attributes
Understand JavaScript WebAPI in one articleCode example 1: Play pause transition.

Running screenshot:

代码示例2: 计数

  

Understand JavaScript WebAPI in one article

代码示例3: 全选/取消全选按钮

选择你喜欢玩的游戏

王者荣耀
和平精英
开心消消乐
我的世界
全选

运行截图
Understand JavaScript WebAPI in one article

3.4 获取/修改样式属性

CSS 中指定给元素的属性, 都可以通过 JS 来修改

style 中的属性都是使用 驼峰命名 的方式和 CSS 属性对应的.
例如:font-size => fontSize,background-color => backgroundColor

1. 行内样式操作

element.style.[属性名] = [属性值];element.style.cssText = [属性名+属性值];

代码示例: 字体变大

你好

运行截图:
Understand JavaScript WebAPI in one article

2. 类名样式操作

element.className = [CSS 类名];

代码示例: 背景颜色变化

 

你好!

运行截图:
Understand JavaScript WebAPI in one article

4. 操作节点

4.1 新增节点

分为两个步骤:

  1. 创建元素节点
    createElement创建元素节点.
    createTextNode创建文本节点
    createComment创建注释节点
    createAttribute创建属性节点
  2. 插入节点到 dom 树中
    ① 使用appendChild将节点插入到指定节点的最后一个孩子之后
    ②使用insertBefore将节点插入到指定节点之前

代码示例:

运行截图:
Understand JavaScript WebAPI in one article

代码示例: 当一个节点插入两次,相当于移动.

1

2

3

4

运行结果:
Understand JavaScript WebAPI in one article

4.2 删除节点

使用removeChild删除子节点

oldChild = element.removeChild(child);

注: 如果 child 不是 element 的子节点,会抛异常

代码示例:

1

2

3

运行结果:
Understand JavaScript WebAPI in one article

5. 实现几个案例

5.1 猜数字

nbsp;html>    猜数字 

请输入要猜的数字:

已经猜的次数: 0

结果:

运行截图:
Understand JavaScript WebAPI in one article

5.2 表白墙

nbsp;html>    表白墙 

表白墙

输入后点击提交,会将信息显示在表格中

谁:

对谁:

说什么:

运行截图:
Understand JavaScript WebAPI in one article

5.3 待办事项

nbsp;html>    待办事项 

未完成

已完成

运行截图:
Understand JavaScript WebAPI in one article

【相关推荐:javascript视频教程web前端

The above is the detailed content of Understand JavaScript WebAPI in one article. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete