目录
一.正则对象 RegExp
1.声明正则表达式的方式一
2.声明正则表达式的方式二
3.匹配数据
4.正则的bug
全局模式的指针移动
匹配数据为空时
5.math对象
二.DOM和BOM
1.什么是DOM和BOM
2.Window对象
(1)open():
(2)close():
(3)setTimeout():
(4)setInterval():
(5)alert():
(6)confirm():
(7)prompt():
(8)innerHeight属性
(9)innerWidth属性
(10)监听resize事件
(11)window.opener
3.Window子对象
(1)window.document:
(2)window.location:
window.location.href:*
window.location.protocol:
window.location.host:
window.location.hostname:
window.location.port:
window.location.pathname:
window.lacation.search:
window.location.hash:
window.location.assign():
window.location.reload():*
window.history:
window.history.length:
window.history.back():*
window.history.forward():*
window.history.go():
window.history.pushState():
window.history.replaceState():
4.查找标签
三.事件
在python中如果需要使用正则,需要借助re模块
在js中需要创建正则对象
let reg = new RegExp
let reg1 = /正则表达式/
let reg = new RegExp(正则表达式)
reg.test(待匹配内容)
// 获取字符串中的某个字母
let str = "dream eam eam eam"
//字符串内置方法
str.match(/m/) // 拿到第一个字母并返回索引,不会继续匹配
str.match(/m/g) // 全局匹配 g表示全局模式
// 获取字符串中的某个字母
let str = "dream eam eam eam"
undefined
str.match(/m/) // 拿到第一个字母并返回索引,不会继续匹配
// ['m', index: 4, input: 'dream eam eam eam', groups: undefined]
str.match(/m/g) // 全局匹配 g表示全局模式
// (4) ['m', 'm', 'm', 'm']
let reg = /^[a-zA-Z][A-Za-z0-9]/g
reg.test("dream");
let reg = /^[a-zA-Z][A-Za-z0-9]/g
// 第一次匹配成功 -- 有数据 -- 指针移动到尾端
reg.test("dream");
// true
//第二次匹配失败 -- 指针在尾端向后匹配 -- 无数据
reg.test("dream");
// false
// 第三次匹配成功 -- 有数据 -- 指针回到头部
reg.test("dream");
// true
reg.test("dream");
// false
// 第二次匹配失败 -- 指针在尾端向后匹配 -- 无数据
reg.lastIndex
// 0
reg.test("dream");
// true
// 第三次匹配成功 -- 有数据 -- 指针回到头部
reg.lastIndex
// 2
let reg = /^[a-zA-Z][A-Za-z0-9]/
reg.test();
let reg = /^[a-zA-Z][A-Za-z0-9]/
// 针对上述表达式:没有数据时,默认传进去的参数是 underfined -- 匹配成功
reg.test();
// true
reg.test();
// true
abs(x) | 返回数的绝对值 |
exp(x) | 返回e的指数 |
floor(x) | 对数进行下舍入 |
log(x) | 返回数的自然对数(底为e) |
max(x,y) | 返沪x和y中的最高值 |
min(x,y) | 返回x和y中的最低值 |
pow(x,y) | 返回x的y次幂 |
random(0,1) | 返回0~1之间的随机数 |
round(x) | 把数四舍五入为最接近的整数 |
sin(x) | 返回数的正弦 |
sqrt(x) | 返回数的平方根 |
tan(x) | 返回角的正切 |
- BOM(Browser Object Model)
- 浏览器对象模型
- js代码操作浏览器
- DOM(Docunment Object Model)
- 文档对象模型
- js代码操作标签
Window对象是JavaScript中表示浏览器窗口的全局对象,它提供了一系列方法来操作和管理窗口
let newWindow = window.open("https://www.baidu.com","_blank");
let newWindow = window.open("https://www.baidu.com","_blank",'height=400px,width=400px');
window.close();
setTimeout(function(){
// 执行代码
},100); // 1秒后执行
setInterval(function(){
// 执行代码
},200); // 每2秒执行一次
window.alert("Hello,World!");
if (window.confirm("Are you sure?")){
// 用户点击了确认按钮
} else {
// 用户点击了取消按钮
}
let name = window.prompt("Please enter your name:");
let windowHeight = window.innerHeight;
console.log(windowHeight); // 输出当前窗口的视口高度
let windowWidth = window.innerWidth;
console.log(windowWidth); // 输出当前窗口的视口宽度
window.addEventListener("resize",function() {
let windowHeight = window.innerHeight;
let windowWidth = window.innerWidth;
console.log("Window Height:", windowHeight)
console.log("Window Width:", windowWidth);
});
在以下情况下可以使用window.opener:
let popup = window.open("popup.html"); // 打开一个弹出窗口
let parentWindow = window.opener;
console.log(parentWindow); // 输出父窗口对象
let currentWindow = window.open("current.html"); // 打开当前窗口
// 向当前窗口发送消息
currentWindow.postMessage("Hello", "http://example.com");
window.addEventListener("message", function(event) {
let message = event.data;
let sourceWindow = event.source;
let openerWindow = window.opener;
console.log("Message:", message);
console.log("Source Window:", sourceWindow);
console.log("Opener Window:", openerWindow);
});
如果是Window的子对象,Window可以不写
通过window.document,可以使用各种方法来查询和修改当前页面的 HTML 结构、CSS 样式和 DOM 元素。
例如,要获取页面标题:
let pageTitle = window.document.title;
console.log(pageTitle);
此子对象包含有关当前页面 URL 的信息,并提供了一些与页面导航相关的方法。
通过window.location,可以获取当前页面的 URL、查询字符串参数、路径等信息,并且可以使用一些方法来导航到其他页面。
例如,要在新标签页中打开一个 URL:
window.location.href = 'https://example.com';
let currentURL = window.location.href;
console.log(currentURL);
window.href = url // 跳转到目标地址
let protocol = window.location.protocol;
console.log(protocol);
let host = window.location.host;
console.log(host);
let hostname = window.location.hostname;
console.log(hostname);
let port = window.location.port;
console.log(port);
let pathname = window.location.pathname;
console.log(pathname);
let searchParams = window.location.search;
console.log(searchParams);
let hash = window.location.hash;
console.log(hash);
// 将当前页面重定向到 example.com
window.location.assign("http://example.com");
// 重新加载当前页面
window.location.reload();
window.history.back();
let historyLength = window.history.length;
console.log(historyLength);
window.history.back();
window.history.forward();
// 后退两个历史记录
window.history.go(-2);
// 前进一个历史记录
window.history.go(1);
// 重新加载当前页
window.history.go();
// 添加新的历史记录状态
window.history.pushState({ page: 1 }, "Page 1", "/page1.html");
// 替换当前历史记录状态
window.history.replaceState({ page: 2 }, "Page 2", "/page2.html");
let userAgent = window.navigator.userAgent;
console.log(userAgent);
该方法可以用来校验反爬程序
let userAgent = window.navigator.userAgent;
console.log(userAgent);
let platform = window.navigator.platform;
console.log(platform);
let vendor = window.navigator.vendor;
console.log(vendor);
let language = window.navigator.language;
console.log(language);
let cookieEnabled = window.navigator.cookieEnabled;
console.log(cookieEnabled);
let plugins = window.navigator.plugins;
console.log(plugins);
直接查找:
document.getElementById | 根据ID获取一个标签 |
document.getElementsByClassName | 根据class属性获取 |
document.getElementsByTagName | 根据标签名获取标签合集 |
间接查找:
parentElement | 父节点标签元素 |
children | 所有子标签 |
firstElementChild | 第一个子标签元素 |
lastElementChild | 最后一个子标签元素 |
nextElementSibling | 下一个兄弟标签元素 |
previousElementSibling | 上一个兄弟标签元素 |
常用事件:
onclick | 当用户点击某个对象时调用的事件句柄。 |
ondblclick | 当用户双击某个对象时调用的事件句柄。 |
onfocus | 元素获得焦点。 |
onblur | 元素失去焦点。(应用场景:用于表单验证,用户离开某个输入框时,代表已经输入完了,我们可以对它进行验证.) |
onchange | 域的内容被改变。 (应用场景:通常用于表单元素,当元素内容被改变时触发.(select联动)) |
onkeydown | 某个键盘按键被按下。 (应用场景: 当用户在最后一个输入框按下回车按键时,表单提交.) |
onkeypress | 某个键盘按键被按下并松开。 |
onkeyup | 某个键盘按键被松开。 |
onload | 一张页面或一幅图像完成加载。 |
onmousedown | 鼠标按钮被按下。 |
onmousemove | 鼠标被移动。 |
onmouseout | 鼠标从某元素移开。 |
onmouseover | 鼠标移到某元素之上。 |
onselect | 在文本框中的文本被选中时发生。 |
onsubmit | 确认按钮被点击,使用的对象是form。 |