js操作页面三步骤
复制<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>操作页面的三步骤</title>
</head>
<body>
<h1>操作页面的三步骤</h1>
<div class="box">
<h1>box h1</h1>
</div>
</body>
<script>
let body = document.querySelector('body');
let box = document.querySelector('.box');
let body_h1 = body.querySelector('h1');
console.log(body_h1);
let box_h1 = box.querySelector('h1');
console.log(box_h1);
body_h1.onclick = function () {
if (box_h1.style.color != 'red') {
box_h1.style.color = 'red';
box_h1.style.backgroundColor = 'orange';
} else {
box_h1.style.color = 'black';
box_h1.style.backgroundColor = 'white';
}
}
</script>
</html>
js事件
鼠标事件
复制<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>鼠标事件</title>
<style>
.box {
width: 200px;
height: 200px;
background-color: pink;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
<script>
let box = document.querySelector('.box');
box.onclick = function () {
console.log('单击了', this)
};
box.ondblclick = function () {
console.log('双击了')
};
box.oncontextmenu = function () {
console.log('右键了');
return false;
};
box.onmouseover = function () {
console.log('悬浮了');
};
box.onmouseout = function () {
console.log('移开了');
};
box.onmousemove = function () {
console.log('移动了');
};
box.onmousedown = function () {
console.log('按下了');
};
box.onmouseup = function () {
console.log('抬起了');
};
</script>
</html>
文档事件
复制<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档事件</title>
<style>
body {
height: 3000px;
}
</style>
<script>
window.onload = function () {
console.log(h1)
}
</script>
</head>
<body>
<h1 id="h1">hhhhh</h1>
</body>
<script>
let body = document.querySelector('body');
document.onscroll = function (ev) {
console.log('滚动了');
if (window.scrollY >= 500) {
body.style.backgroundColor = 'red';
} else {
body.style.backgroundColor = 'white';
}
}
</script>
</html>
键盘事件
复制<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>键盘事件</title>
</head>
<body>
<h1>键盘事件</h1>
<input type="text">
</body>
<script>
let inp = document.querySelector('input');
inp.onkeydown = function () {
console.log('按下')
};
inp.onkeyup = function () {
console.log('抬起')
}
</script>
</html>
表单事件
复制<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>表单事件</title>
<style>
input {
border: 2px solid pink;
}
input:focus {
outline: 2px solid yellow;
}
</style>
</head>
<body>
<form action="">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="登录">
</form>
</body>
<script>
let form = document.querySelector('form');
let submit = document.querySelector('[type="submit"]');
let usr = document.querySelector('[type="text"]');
form.onsubmit = function () {
console.log('提交了');
return false;
};
usr.onfocus = function () {
console.log('获取焦点')
};
usr.onblur = function () {
console.log('失去焦点')
};
</script>
</html>
事件对象
复制<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>事件对象</title>
</head>
<body>
<input type="text" class="inp">
</body>
<script>
inp = document.querySelector('.inp');
inp.onkeydown= function (ev) {
console.log(ev);
if (ev.keyCode === 13) {
console.log('回车了')
}
if (ev.ctrlKey && ev.keyCode === 13) {
console.log('消息发送了')
}
};
document.onclick = function (ev) {
console.log(ev);
console.log(ev.clientX, ev.clientY);
}
</script>
</html>
js操作内容
复制<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>内容操作</title>
</head>
<body>
<h1 class="title">标题</h1>
<input type="text" class="title">
<button class="btn">改标题</button>
</body>
<script>
let h1 = document.querySelector('h1.title');
let inp = document.querySelector('input.title');
let btn = document.querySelector('.btn');
btn.onclick = function () {
inp_value = inp.value;
if (inp_value) {
inp.value = '';
h1.innerHTML = inp_value;
}
}
</script>
</html>
js操作样式
复制<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>样式操作</title>
<style>
.box {
width: 200px;
height: 200px;
background-color: pink;
}
.sup-box {
height: 100px;
background-color: orange;
border-radius: 50%;
line-height: 100px;
text-align: center;
color: red;
}
</style>
</head>
<body>
<div class="box">文本</div>
</body>
<script>
let box = document.querySelector('.box');
box.onclick = function () {
console.log(this.className);
if (this.className === 'box') {
this.className += ' sup-box';
} else {
this.className = 'box';
}
};
</script>
</html>
页面转跳
复制<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>页面转跳</title>
</head>
<body>
<button class="b1">自我刷新</button>
<button class="b2">转跳到9</button>
<button class="b3">ctrl新开转跳到9</button>
</body>
<script>
window.owen = 'Owen';
let b1 = window.document.querySelector('.b1');
b1.onclick = function () {
location.reload();
};
let b2 = window.document.querySelector('.b2');
b2.onclick = function () {
window.location.href = '9、样式操作.html';
};
let b3 = window.document.querySelector('.b3');
b3.onclick = function (ev) {
if (ev.ctrlKey) {
window.open('9、样式操作.html');
} else {
window.open('9、样式操作.html', '_self');
}
}
</script>
</html>
屏幕有滚动条下的两种宽度
去除滚动条剩余的全部宽度
复制let html = document.querySelector('html');
console.log(html.clientWidth);
不去除滚动条剩余的全部宽度
复制function getHtmlWidth() {
let hidden = document.createElement('div');
hidden.style.width = '100vw';
html.appendChild(hidden);
width = parseInt(getComputedStyle(hidden, null).width);
html.removeChild(hidden);
return width
}
width = getHtmlWidth();
console.log(width);
案例:动态尺寸
复制<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>动态尺寸</title>
<style>
body {
margin: 0;
height: 3000px;
}
.box {
background-color: orange;
position: fixed;
top: 0;
left: 0;
min-width: 900px;
max-width: 1100px;
width: 90%;
margin-left: 5%;
}
</style>
</head>
<body>
<div class="hidden" style="width: 100vw"></div>
<div class="box"></div>
</body>
<script>
let html = document.querySelector('html');
console.log(html.clientWidth);
function getHtmlWidth() {
let hidden = document.createElement('div');
hidden.style.width = '100vw';
html.appendChild(hidden);
width = parseInt(getComputedStyle(hidden, null).width);
html.removeChild(hidden);
return width
}
width = getHtmlWidth();
console.log(width);
function resizeBox() {
box_width = parseInt(getComputedStyle(box, null).width);
box.style.height = box_width / 6 + 'px';
if (box_width >= 1100) {
box.style.marginLeft = (html.clientWidth - 1100) / 2 + 'px'
}
}
let box = document.querySelector('.box');
resizeBox();
window.onresize = function () {
resizeBox();
};
</script>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理