what is javascript bom
In JavaScript, BOM refers to the Browser Object Model (Browser Object Model), which provides objects that interact with the browser window independently of the content. It is mainly used to manage the interaction between windows. Communication, its core object is window.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
1. What is BOM
BOM (Browser Object Model) is the browser object model.
BOM provides objects that interact with the browser window independently of content;
Since BOM is mainly used to manage windows and windows communication between each other, so its core object is window;
BOM is composed of a series of related objects, and each object provides many methods and properties;
BOM lacks standards. The standardization organization for JavaScript syntax is ECMA, and the standardization organization for DOM is W3C. BOM was originally part of the Netscape browser standard.
2. What to learn when learning BOM
We will learn some objects that interact with the browser window, such as The window object that can move and resize the browser, the location object and history object that can be used for navigation, the navigator and screen objects that can obtain browser, operating system, and user screen information, and the document can be used as the entrance to access and manage HTML documents. The frames object of the frame, etc. Here, I only introduce some basic knowledge of window objects, etc., and some ECMAscript knowledge will also be explained. Other objects Location, Screen, Navigator, and History are not introduced in detail one by one. .
BOM structure diagram
##3. Window object
The window object is the top-level object in js. All variables and functions defined in the global scope will become the properties and methods of the window object. You can omit the window when calling. Example: Open the window1 2 3 4 5 |
|
1 |
|
4. BOM fragmentary knowledge (window object)
1. Timer
Delayed execution
1 2 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Timing execution
1 2 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
2.offset series method
##offsetLeft and offsetTopoffsetLeft Form #1, to the left/top of the nearest (positioned) parent elementThe difference between offsetLeft and style.left1, style.left can only get inline stylesoffsetWidth and offsetHeight | The composition of offsetHeight | offsetHeight = height padding border offsetWidth is the same |
##The difference between offsetHeight and style.height | 1. demo.style.height can only obtain inline styles, otherwise it cannot be obtained 2. .style.height is a string (with unit px), offsetHeight is a numerical value (without unit) 3. .style. height can set the inline style, but offsetHeight is a read-only attribute and cannot be set So: demo.style.height gets the real height/width of an element, and uses .style.height to set it Height/Width | |
2, if all parent elements are not positioned, the body will prevail 3, offsetLeft is the distance from the left side of its own border to the left side of the parent padding | ||
| 2,offsetLeft只读,style.left可读可写 3,offsetLeft是数值,style.left是字符串并且有单位px 4,如果没有定位,style.left获取的数值可能是无效的 5,最大的区别:offsetLeft以border左上角为基准, style.left以margin左上角为基准 | |
offsetParent | 构成 | 1. 返回该对象距离最近的带有定位的父级元素 2. 如果当前元素的所有父级元素都没有定位(position为absolute或relative),那么offsetParent为body 3. offsetLeft获取的就是相对于offsetParent的距离 |
| 与parentNode的区别 | parentNode始终指向的是当前元素的最近的父元素,无论定位与否 |
offset示意图
3.scroll系列方法
scrollHeight和scrollWidth | 对象内部的实际内容的高度/宽度(不包括border) |
scrollTop和scrollLeft | 被卷去部分的顶部/左侧 到 可视区域 顶部/左侧 的距离 |
onscroll事件 | 滚动条滚动触发的事件 |
页面滚动坐标 | var scrollTop = window.pageYoffset || document.documentElement.scrollTop || document.body.scrollTop || 0; |
scroll示意图
4.client系列
clientX和clientY 获取鼠标在可视区域的位置 clientX = width + padding,clientY = height + padding
clientLeft 边框的宽度,若有滚动条的话,包括滚动条
client示意图
例: 获得页面可视区域的大小
1 2 3 4 5 6 |
|
5.事件参数e
当事件发生的时候,系统会自动的给事件处理函数传递一个参数,会提供事件相关的一些数据,事件参数e浏览器的兼容性检测: e = e || window.event
e.pageX和e.pageY | 获取鼠标在页面中的位置(IE8中不支持pageX和pageY,支持window.event获取参数事件) | pageX = clientX + 页面滚动出去的距离 |
6.获得计算后样式的方法
w3c标准 | window.get ComputedStyle(element, null)[属性] |
IE浏览器 | element.currentStyle[属性] |
封装浏览器兼容性函数 | function getStyle(element, attr) { if(window.getComputedStyle) { return window.getComputedStyle(element, null)[attr]; } else { return element.currentStyle[attr]; } } |
7.事件补充
- 注册事件
- 注册事件的性能问题
- 移除事件
- 事件冒泡
- 事件捕获 事件的三个阶段
- 事件对象的常见属性
DOM笔记里有提到注册事件和移除事件,这里着重讲事件对象,事件对象的常见属性
7.1 target 和currentTarget
target | 始终是点击的元素(IE8及之前是srcElement) |
currentTarget | 执行事件处理函数的元素 |
this | 始终和currentTarget一样 |
7.2 事件冒泡
用addEventListener注册事件的时候,第三个参数是false,即是冒泡。
冒泡的好处 - 事件委托
从一个例子中说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
当事件冒泡影响到了其他功能的实现时,需要阻止冒泡
e.stopPropagation( ) | IE8及之前: event.cancleBubble = true; |
阻止默认行为的执行
e.preventDefault() | IE8及之前: event.returnValue = false; |
看一下阻止跳转到百度的例子:
1 2 3 4 5 6 7 8 9 10 11 |
|
7.3 鼠标事件的参数
e.type | 事件的类型,如click,mouseover |
事件的3个阶段 | 1 捕获阶段 2 目标阶段 3 冒泡阶段 |
e.eventPhase | 事件阶段 |
shiftKey/ctrlKey/altKey | 按下鼠标同时按下组合键 |
button | 获取鼠标的按键 |
e.clientX和e.clientY | 获取鼠标在可视区域的位置 |
还有7.2中的取消事件冒泡和阻止默认行为的执行
8.正则表达式
定义:
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。
规则:
1 元字符 | . 匹配任何单个字符,除了换行 d 数字 \D 非数字 [0-9] w 数字 字母 下划线 \W 非 [0-9a-zA-Z_] \s 空白 \S 非空白 \n 换行 \t 制表符 |
2 范围-- 匹配的是一个字符 | [0-9] [0123] [a-z] [A-Z] 匹配的是一个字符 |
3 | 或者 | | 或者 |
4 量词 -只修饰一个字符 | a+ 1个或多个a a? 1个或0个a a* 0个或多个a a{x} x个n a{x,} 至少x个a a{x,y} x-y个a |
5 开始结束 | ^a 以a开始 a$ 以a结束 |
6 ( ) 看成是一个整体,即分组 | |
7 匹配汉字 | [\u4e00-\u9fa5] |
8 参数 | i 忽略大小写 g 全局匹配 |
9 ^在[ ]中的作用——取反 | |
10 贪婪模式和非贪婪模式 | 默认情况 贪婪模式 <.+> 非贪婪模式 <.+?> |
8.1 正则表达式对象RegExp
1 2 3 4 5 6 7 8 9 |
|
8.2 正则之匹配
例:验证电子邮箱
1 2 3 4 5 6 7 8 |
|
8.3 正则之提取
例:找数字
1 2 3 4 5 |
|
8.4 正则之替换
例:所有的逗号替换成句号
1 2 3 |
|
8.5 正则的分组( )
在正则表达式中用( )把要分到一组的内容括起来,组分别是RegExp.$1 RegExp.$2等等
例:交换位置 源字符串"5=a, 6=b, 7=c" 要的结果"a=5, b=6, c=7"
1 2 3 |
|
9.键盘事件对象
方法 keydown 按下时 keypress 按下 keyup 抬起时 | 属性
keyCode 键盘码,只有数字和字母对应ASCII码 charCode 对应ASCII码,只有在keypress中才生效(IE9+) |
例:在切换鼠标焦点时,用enter键代替tab键
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
补充:js中的instanceof运算符介绍
判断某个变量是不是某种类型的对象
1 2 3 4 |
|
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of what is javascript bom. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
