当前位置:网站首页>Web API——获取元素、事件基础
Web API——获取元素、事件基础
2022-07-13 17:47:00 【mintsolace】
1. 如何获取页面元素
DOM在实际开发中主要用来操作元素
获取页面中的元素可使用以下几种方式:
(1)根据ID获取
(2)根据标签名获取
(3)根据HTML5新增的方法获取
(4)特殊元素获取
2. 根据ID获取
使用getElementById()方法可以获取带有ID的元素对象
element是一个Element对象,如果当前文档中拥有特定ID的元素不存在则返回null
id是大小写敏感的字符串,代表了所要查找的元素的唯一ID
//返回的是获取过来元素对象的集合,以伪数组的形式存储
document.getElementsByTagName('li');//可以得到这个元素里面的某些标签
var nav = document.getElementById('nav');//ID只有一个,获得的是这个nav元素
getElementsByClassName根据类名获得某些元素集合
document.getElementsByClassName('box');
querySelector返回指定选择器的第一个元素对象
var firstBox = document.querySelector('.box');
querySelectorAll()返回指定选择器所有元素对象集合
var allBox = document.querySelectorAll('.box');
3.获取特殊元素(body, html)
var bodyEle = document.body;//获取body元素
var htmlEle = document.documentElement;//获取html元素
事件基础:触发响应机制。
Javascript使我们有能力创建动态页面,事件是可以被Javascript侦测到的行为。
网页中每个元素都可以产生某些可以触发Javascript的事件,例如可以在用户点击某个按钮时产生一个事件,然后去执行某些操作。
事件三要素:即事件由三部分组成:事件源,事件类型,事件处理程序。
事件源:事件被触发的对象,按钮。
事件类型:如何触发,什么事件:鼠标点击还是经过或者按下?
事件处理程序:通过一个函数赋值的方式完成
边栏推荐
- MSF利用永恒之蓝渗透win2003
- Reflection get member methods and member variables
- 蚂蚁集团开源大规模视频侵权定位数据集
- 7. Cache breakdown, cache penetration, cache
- Summer precipitation Web Learning -- PHP Foundation
- Componentized coding process -- todo list case
- Vue+axios+mysql realizes paging query, condition query and batch deletion
- 开源实习 | Compiler SIG 实习任务正式发布,欢迎报名申请!
- BUUCTF webshell后门
- MySQL-DQL-条件查询/聚合函数/分组查询/排序查询/分页查询
猜你喜欢

Unity experiment - simulating the motion of stars in the solar system

开源实习 | Compiler SIG 实习任务正式发布,欢迎报名申请!

sniffer Pro對ARP協議的分析、捕獲與模擬攻擊

Infrastructure nftscan officially released Solana network NFT browser

服务器对接码云webhooks实现自动部署--超详细--PHP

BUUCTF 来首歌吧

MSF infiltrates Win2003 with eternal blue

编译原理-词法分析器设计

Intranet penetration notes --:) a smiling face

Intranet penetration notes - vulnhub intranet course completion penetration
随机推荐
unity实验-重力撞墙
MySQL multi table query internal connection / external connection / self connection
39.js-- scope
关于 Visual Studio 2022的安装与使用
Graphic and image programming practice course report
Hand to hand teaching - using native POI to import and export excel and the basic operations of Alibaba's easyexcel
[node] node create server +mysql store data minimalist demo
[BJDCTF2020]Cookie is so stable
sniffer Pro對ARP協議的分析、捕獲與模擬攻擊
内网渗透笔记——二层发现
MSF利用永恒之蓝渗透win2003
Buuctf nine links
内网渗透笔记——:)一个笑脸
Find a value of tree data
BUUCTF 荷兰宽带数据泄露
Network security emergency response malicious code analysis technology
Oracle learning
[Vue] keep alive the simplest way to clear the cache
Componentized coding process -- todo list case
For some problems encountered in using crontab, errors are reported /var/spool/cron: permission denied and bash: /usr/bin/chattr: permission denied