Home>Article>Web Front-end> Understand JavaScript WebAPI in one article
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.
[Related recommendations:javascript video tutorial,web front-end】
JS is divided into three major parts:
WebAPIincludesDOM BOM.
DOMstands forDocument Object Model
.
W3CThe standard provides us with a series of functions that allow us to operate:
The structure of a page is a tree structure, calledDOM tree.
.
to represent.
to represent.
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);
Fill in one or more selectors
Usage example:
abc
def
Run screenshot
##2.2 querySelectorAll
Run screenshot123
456
3. Manipulate elements
Attribute represents the "rendered" text content of a node and its descendants
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:
Run screenshothello world
hello world
:Passed
innerTextUnable to get
to thepinternalhtml
structure, only the text content can be obtained.
2. innerHTML
The property sets or gets the descendants of the element represented by HTML syntaxNote
:
Run screenshot:hello world
hello world
innerHTML
Not only can you get thehtml
structure of the page, but you can also modify the structure. And the content you get is retained. Spaces and line breaks
3.2 Get/modify element attributes
Get attributes# throughelement.properties##Code example:
Run result:
##3.3 Get/modify form element attributes
Code example 1: Play pause transition.
选择你喜欢玩的游戏
王者荣耀
和平精英
开心消消乐
我的世界
全选
运行截图
CSS 中指定给元素的属性, 都可以通过 JS 来修改
style 中的属性都是使用 驼峰命名 的方式和 CSS 属性对应的.
例如:font-size => fontSize
,background-color => backgroundColor
等
element.style.[属性名] = [属性值];element.style.cssText = [属性名+属性值];
你好
运行截图:
element.className = [CSS 类名];
你好!
运行截图:
分为两个步骤:
createElement
创建元素节点.createTextNode
创建文本节点createComment
创建注释节点createAttribute
创建属性节点appendChild
将节点插入到指定节点的最后一个孩子之后insertBefore
将节点插入到指定节点之前运行截图:
1
2
3
4
运行结果:
使用removeChild
删除子节点
oldChild = element.removeChild(child);
注: 如果 child 不是 element 的子节点,会抛异常
1
2
3
运行结果:
nbsp;html>猜数字
请输入要猜的数字:
已经猜的次数: 0
结果:
运行截图:
nbsp;html>表白墙
表白墙
输入后点击提交,会将信息显示在表格中
谁:
对谁:
说什么:
运行截图:
nbsp;html>待办事项
未完成
已完成
运行截图:
【相关推荐: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!