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>
// 1、获取页面标签
// 2、设置操作的激活条件 - 事件
// 3、具体的操作方式 - 内容 | 样式 | 事件 | 文档结构 // 1
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); // 2
body_h1.onclick = function () {
// console.log('你丫点我了')
// 3
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('右键了');
// 有些事件有系统默认动作,取消默认动作可以返回 false
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('滚动了');
// console.log(ev);
// console.log(window.scrollY);
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);
// console.log(ev.keyCode); 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'); // 内容操作:value | innerText | innerHTML
btn.onclick = function () {
// 拿到输入框的内容
inp_value = inp.value;
if (inp_value) {
// inp_value = ''; // 改的只是一个内存变量
inp.value = ''; // 清空输入框 // 将内容赋值给h1 innerText | innerHTML
// console.log(h1.innerText);
// console.log(h1.innerHTML);
// 纯文本
// h1.innerText = 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 {
/*width: 400px;*/
height: 100px;
background-color: orange;
border-radius: 50%;
line-height: 100px;
text-align: center;
color: red;
}
</style>
</head>
<body>
<!--<div class="box" style="background-color: deeppink;"></div>-->
<div class="box">文本</div>
</body>
<script>
let box = document.querySelector('.box');
// 需求1:单击获取标签的之前背景颜色
/*
box.onclick = function () {
// 注:this.style 本质操作的是行间式(只能获取和设置行间式)
// bgColor = this.style.backgroundColor;
// console.log(bgColor); // 注:在内联和外联中书写的样式称之为 计算后样式 // 注:getComputedStyle 能获取计算后样式,也能获取行间式,但是只读
// getComputedStyle(标签, 伪类).样式;
bgColor = getComputedStyle(this, null).backgroundColor;
console.log(bgColor);
width = getComputedStyle(this, null).width;
console.log(width, parseInt(width)); // 只读,会报错
// getComputedStyle(this, null).backgroundColor = 'rgb(255, 20, 147)';
}
*/ // 需求2:点击修改标签的宽高背景颜色
/*
box.onclick = function () {
this.style.backgroundColor = 'orange';
this_style = getComputedStyle(this, null);
// console.log(parseInt(this_style.width) * 2);
// 宽放大两倍,高缩小两倍
this.style.width = parseInt(this_style.width) * 2 + 'px';
this.style.height = parseInt(this_style.height) / 2 + 'px';
}
*/ // 需求:操作计算后样 - 提取写好计算后样式,通过类名将 js 与 css 建立关联
box.onclick = function () {
console.log(this.className);
// this.className = 'sup-box'; /*
if (this.className === 'box') {
this.className = 'sup-box';
} else {
this.className = 'box';
}
*/
// 注:有个空格:空格sup-box
// this.className += ' sup-box'; 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 () {
// console.log(owen); // '' 代表当前页面链接
// window.location.href = '';
location.reload();
}; let b2 = window.document.querySelector('.b2');
// 转跳到9*.html
b2.onclick = function () {
// 在自身所在标签替换
window.location.href = '9、样式操作.html';
}; // ctrl新开转跳到9
let b3 = window.document.querySelector('.b3');
b3.onclick = function (ev) {
// open('转跳路径', '默认就是_blank')
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 {
/*width: 200px;*/
/*height: 200px;*/
/*width: 100%;*/ background-color: orange;
position: fixed;
top: 0;
left: 0; min-width: 900px;
max-width: 1100px; width: 90%;
margin-left: 5%; /*vw viewwidth vh viewheight*/
/*width: 90vw;*/
/*margin-left: 5vw;*/
}
</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); // 包含滚动条的宽度
// let hidden = document.querySelector('.hidden');
// width = parseInt(getComputedStyle(hidden, null).width);
// console.log(width); 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>

js基本操作的更多相关文章

  1. two.js基本操作

    今天介绍一个网络上并不常用的插件two.js,two.js是一款网页二维绘图软件,可以在指定区域内产生自设的各种动画效果 下载网址如下: https://two.js.org/#download 一: ...

  2. JS 基本操作

    1.判断数据是否包含某些数据 var ary=[{age:20,name:"a"},{age:20,name:"b"},,{age:30,name:" ...

  3. 模仿WC.exe的功能实现--node.js

    Github项目地址:https://github.com/102derLinmenmin/myWc WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要 ...

  4. IOS-网络(网页开发-UIWebView,HTML,CSS,JavaScript,OC和JS代码互调)

    一.网页基础 // // ViewController.m // IOS_0218_网页开发1 // // Created by ma c on 16/2/18. // Copyright © 201 ...

  5. 如何让 JS 代码不可断点

    绕过断点 调试 JS 代码时,单步执行(F11)可跟踪所有操作.例如这段代码,每次调用 alert 时都会被断住: debugger alert(11) alert(22) alert(33) ale ...

  6. vue中解决时间在ios上显示NAN的问题

    最近在用vue,遇到倒计时在ios上显示为NAN的问题. 因为做的是倒计时支付,思路是获取服务器时间和下单时间,再转成秒级时间戳做差值. 在网上找到说是ios 不支持例如2018-09-01 10:0 ...

  7. js数组(列表)的基本操作

    本文主要介绍JS对数组(列表)的基本操作.习惯了用数据库的操作顺序来说明:增.删.改.查:合并,裁剪,排序,格式化. 一.数组元素的添加(增加) 增加数组元素有三种方法:unshift()  push ...

  8. H5JS二维动画制作!two.js的基本操作class1

    今天介绍一个网络上并不常用的插件two.js,刚开始学习的过程中,发现网上并没有合适的教程,在此发表基本操作 two.js是一款网页二维绘图软件,可以在指定区域内产生自设的各种动画效果 下载网址如下: ...

  9. js对WebApi请求的基本操作

    在WebAPI对外提供的,大概有4种接口,get,post,delete,put,现在,我就简单的来说一下js请求webApi的方式和大概的作用: get:在webApi中,get方法通常是用来获取数 ...

随机推荐

  1. CouchDB学习-集群管理

    官方文档 集群管理 理论 在etc/fefault.ini文件中有以下部分: [cluster] q=8 n=3 q - 分片的数量 n - 每一份文档的拷贝数量(加上原文档一共几份副本) 创建数据库 ...

  2. .net(C#数据库访问) Mysql,Sql server,Sqlite,Access四种数据库的连接方式

    便签记录Mysql,Sql server,Sqlite,Access四种数据库的简单连接方式 //using MySql.Data.MySqlClient; #region 执行简单SQL语句,使用M ...

  3. docker学习笔记---基本命令

    [root@docker ~]# docker Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Opt ...

  4. VNC连接CentOS7远程桌面

    1.在centos7安装图形化 先安装图形用户接口X Window System,再安装GNOME桌面. [root@centos7 ~]# yum groupinstall -y "X W ...

  5. 【STM32H7教程】第34章 STM32H7的定时器应用之TIM1-TIM17的PWM实现

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第34章       STM32H7的定时器应用之TIM1-T ...

  6. 2019icpc上海站 打星体验,首次感想 D K代码

    暑期训练以来首次参赛,体验还行吧,过程有些战战兢兢. 刚开始以为是正式队,热身赛只过了一题,有很大的压力,正赛前一天晚上才知道是打星队,有点如释重负的感觉.也幸好都是打星队,不然真的有可能打铁,虽然实 ...

  7. vivo web service:亿万级规模web服务引擎架构

    本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/ovOS0l9U5svlUMfZoYFU9Q vivo web service是开发团队围绕奇点 ...

  8. git clone 仓库的部分代码

    对于较大的代码仓库来说,如果只是想查看和学习其中部分源代码,选择性地下载部分路径中的代码就显得很实用了,这样可以节省大量等待时间. 比如像 Chromium 这种,仓库大小好几 G 的. clone ...

  9. Ubuntu 创建 Pycharm 桌面快捷方式

    1. 安装 Pycharm,我的运行目录是: /opt/pycharm-/bin 2. 在 /usr/share/applications/ 下创建文件 pycharm.desktop [Deskto ...

  10. (转)两种高效过滤敏感词算法--DFA算法和AC自动机算法

    原文:https://blog.csdn.net/u013421629/article/details/83178970 一道bat面试题:快速替换10亿条标题中的5万个敏感词,有哪些解决思路? 有十 ...