选择器

// 什么是 HTML 以及怎样使用 HTML 编写网页
// 网页的结构是怎样
// 什么是 CSS 以及怎样使用 CSS
// 如何在网页中引入 JavaScript 代码
// 什么是 DOM, 常用 DOM API 使用
// document object model
// application program interface
// 什么是事件, 如何绑定事件
//
// 应该都能看懂, 不懂的稍微做个笔记, 等上课讲解// 自己定义一个 log 函数
var log = function() {console.log.apply(console, arguments)
}// 在 html 文件中, js 代码放在 script 标签中
// 我们把 script 标签放到 body 最后面, 原因上课解释
// 浏览器载入这个 html 文件后
// 会执行我们写在 script 标签中的代码
log('代码开始')/*
DOM(文档对象模型) 是浏览器对 html 文件的描述方式
DOM API 是浏览器提供给 JavaScript 操作 html 页面内元素的方式
简而言之, 浏览器提供了一些内置函数来让我们操作页面(增删改查)
*/// 查找元素
// ===
//
// 查找元素使用 document.querySelector() 函数
// 这个函数的参数是一个选择器(和 CSS 选择器一样)
// 选择器语法和 CSS 选择器一样, 现在只用 3 个基础选择器
// 元素选择器
var body = document.querySelector('body')
// class 选择器, 用的是   .类名
var form = document.querySelector('.login-form')
// id 选择器, 用的是   #id
var loginButton = document.querySelector('#id-button-login')
// log 出来看看
log(body, form, loginButton)// 操作元素的默认属性
// ===
//
// 获得特定属性值
// 注意, getAttribute 只能得到默认值,而不是得到当前的值
// 当前的值可能是被改动后的值
var user = document.querySelector('#id-input-username')
var userValue = user.getAttribute('value')
log('user value', userValue)
// 不存在的属性会返回 null, 它在 js 中代表不存在
log('没有的属性', user.getAttribute('不存在'))
//
// 设置特定属性值
user.setAttribute('value', '新用户名')// 查询属性是否存在
log(user.hasAttributes())       // 查看元素是否有属性
log(user.hasAttribute('value')) // 查看元素是否有特定属性// 删除某个属性
user.removeAttribute('type')// 获得所有属性
var attributes = user.attributes
log('所有属性', attributes)// 操作元素(创建, 删除, 修改)
// ===
//
// 用 document.createElement 函数创建一个元素
var button = document.createElement('button');
// 用 innerHTML 设置属性
button.innerHTML = '注册用户'// 修改
// 用 appendChild 给一个元素添加子元素
// 这里我们给 .login-form 添加刚才创建好的按钮
var form = document.querySelector('.login-form')
form.appendChild(button)
//
// 删除元素
var pwd = document.querySelector('#id-input-password')
// 以下两种方法都可以删除元素
// 一种是自毁
// 一种是父节点删除子元素
pwd.remove()
// form.removeChild(pwd)// 事件
// ===
//
// 事件是用来处理响应的一个机制
// 这个响应可以来自于用户(点击, 鼠标移动, 滚动)
// 也可以来自于浏览器
//
// 下面的链接描述了所有事件, 不过我们先从最常用的事件学起, click 事件
// https://developer.mozilla.org/en-US/docs/Web/Events
//
// 常用例子, 给按钮添加一个点击的事件
// 1, 获得按钮
var loginButton = document.querySelector('#id-button-login')
// 2, 声明一个函数, 用于在按钮点击后执行
var clicked = function(event) {log('按钮被点击到了', event)
}
// 3, 添加事件, 使用 addEventListener 函数, 它有两个参数
// 第一个是事件的名字, 第二个是事件发生后会被自动调用的函数
// loginButton.addEventListener('click', clicked)
// loginButton 发生了 'click' 事件后调用 clicked 函数
//
// 添加完成, 可以自己在浏览器试试, 记得打开 console// 批量添加事件
var buttons = document.querySelectorAll('button')
for (var i = 0; i < buttons.length; i++) {var button = buttons[i]button.addEventListener('click', function(event){var self = event.targetif (self.innerHTML == '注册用户') {log('注册按钮')} else {log('登录按钮')}console.log('循环批量添加click事件', self.id)})
}//
// 复杂的例子,添加选项卡效果(看不懂下节课就能看懂了)
// 给多个元素挂上同一个事件
// 选择多个元素使用函数 querySelectorAll
var buttons = document.querySelectorAll('.radio-button')
// 循环遍历每个元素, 并且绑定点击事件
for (var i = 0; i < buttons.length; i++) {var button = buttons[i]button.addEventListener('click', function(event){// 注意, 这次我们直接定义了函数作为参数传递, 这样做是合法的// 另外, 我们增加了一个 event 参数// 浏览器会给事件响应函数传递一个参数, 它代表了事件本身// 我们可以用 event.target 取出响应事件的元素var self = event.target// clearActive 函数是我们自己定义的// 目的是删除其他元素的 active classclearActive()// add 可以增加一个 classself.classList.add('active')})
}
//
var clearActive = function() {var s = document.querySelector('.active')if (s != null) {// 使用 classList 可以访问一个元素的所有 class// remove 可以删除一个 classs.classList.remove("active")}
}

 

转载于:https://www.cnblogs.com/cuzz/p/8283090.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278614.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue3打包后无法加载页面

1、配置输出路径 // vue.config.js module.exports {publicPath: ./ }2、不能使用history路由 // ... export default new Router({// mode: history, routes: [{path: /,name: home,component: Home}] })

如何使用Avira Rescue CD清洁感染的PC

When you’ve got a PC completely infected with viruses, sometimes it’s best to reboot into a rescue disc and run a full virus scan from there. Here’s how to use the Avira Rescue CD to clean an infected PC. 当您的PC完全感染了病毒时&#xff0c;有时最好重新…

汇编语言第二章总结

第二章 寄存器 (1) 字数据在寄存器中的存放 一个字由两个字节组成&#xff0c;可以存在一个16位寄存器中。 字的高8位 → 存放于通用寄存器的高8位寄存器 字的低8位 → 存放于通用寄存器的低8位寄存器。 例&#xff1a;十进制数据&#xff1a; 20000 → AX 对应的二进制…

Vue组件的三种调用方式

最近在写fj-service-system的时候&#xff0c;遇到了一些问题。那就是我有些组件&#xff0c;比如Dialog、Message这样的组件&#xff0c;是引入三方组件库&#xff0c;比如element-ui这样的&#xff0c;还是自己实现一个&#xff1f;虽然它们有按需引入的功能&#xff0c;但是…

axios把post的RequestPayload格式转为formdata

方法一&#xff1a;配置transformRequest&#xff0c;缺点&#xff1a;其他请求格式的数据也会被重新格式化&#xff08;PUT&#xff0c;PATCH&#xff09; const service axios.create({//设置axios为form-data 方法1// headers: {// post: {// "Content-T…

火狐打印预览_将打印和打印预览命令添加到Firefox的上下文菜单

火狐打印预览Have you been thinking about how much easier it would be to having the Print & Print Preview commands in Firefox’s Context Menu? The Print Context Menu extension for Firefox allows you to avoid having to use the File Menu to access the pr…

每个人都要在自己的“时区”里找到自己的快乐

祝小妹和自己生日快乐&#xff0c;人人都想快乐&#xff0c;却在平常的365天闷闷不乐&#xff0c;但愿家人朋友在平常的每一天都很够健康快乐&#xff01; 在我那个开不了机的手机记事薄有句话还记得&#xff1a;你们不要刻意等我&#xff0c;因为可能现在的我还没来得及去发现…

《2017 云计算评测报告》:带你了解 AWS、阿里云、腾讯云等八家云计算服务提供商的综合用户体验情况...

报告电子版至听云官方博客下载&#xff1a;http://blog.tingyun.com/web/article/detail/1352 评测说明 评测目标&#xff1a;同一应用&#xff08;网站&#xff09;在不同云上的用户访问体验&#xff0c;以及对云资源的使用 洞察周期及范围&#xff1a;2017年4月-2017年9月 访…

js以变量为键

let key "dynamic",obj{[key]:true }; obj[key2]key console.log(obj)一般在配置文件中应用较多

搭建jenkins实现自动化部署

参考&#xff1a; https://www.cnblogs.com/rslai/p/8135460.html转载于:https://www.cnblogs.com/lihuanhuan/p/10612123.html

python 新闻摘要_每日新闻摘要:Microsoft内部禁止应用程序,这样就可以了

python 新闻摘要Recently, a list of apps that Microsoft prohibits for internal employee use leaked, including Slack, Grammarly, and others. It’s tempting to think these are the actions of a company hating competition, but the truth is more complicated. 最近…

vue使用process.env搭建自定义运行环境

一、vue-cli项目下默认有三种模式&#xff1a; development&#xff1a;在 vue-cli-service serve 时使用。production&#xff1a;在 vue-cli-service build 和 vue-cli-service test:e2e 时使用。test&#xff1a;在 vue-cli-service test:unit 时使用。 对应的 process.env…

bootstrap评分插件 Bootstrap Star Rating Examples

http://www.jq22.com/demo/bootstrap-star-rating-master201708041812/ 转载于:https://www.cnblogs.com/waw/p/8288951.html

http 请求报文

1、报文 2、http请求方法 restful接口 post&#xff1a;创建 put&#xff1a;更新 转载于:https://www.cnblogs.com/mengfangui/p/10171559.html

chrome硬件加速_如何在Chrome中打开和关闭硬件加速

chrome硬件加速Google Chrome comes equipped with hardware acceleration, a feature which takes advantage of your computer’s GPU to speed up processes and free vital CPU time. However, sometimes driver incompatibilities can cause this feature to misbehave an…

春节您“抢票”到手了吗,如果没,请进来看看!

不是为了卖“广告”!我与软件作者从不认识&#xff01;我与软件作者因为抢票认识&#xff0c;不&#xff0c;只认识他写的软件&#xff01;51CTO博客2.0后&#xff0c;我一直没有写博文&#xff01;主要原因&#xff1a;不能用Live Writer写博文&#xff0c;复制&#xff0c;粘…

两个矩阵相加 Exercise08_05

1 import java.util.Scanner;2 /**3 * author 冰樱梦4 * 时间&#xff1a;2018年12月5 * 题目&#xff1a;两个矩阵相加6 *7 */8 public class Exercise08_05 {9 public static void main(String[] args){ 10 Scanner inputnew Scanner(System.in); 11 …

vue element form中input等组件不能输入值

<el-input v-model"form.inputVal " />由于data中form只是一个空对象{}&#xff0c;当主动设置 form.inputVal “” 后input却仍无法输入值&#xff0c;这是因为inputVal 属性没有get和set&#xff0c;需要用vue内置属性设置&#xff1a;this.$set(this.form,…

如何在PowerPoint中制作三折

While Microsoft PowerPoint is almost exclusively used for presentation purposes, it’s also a great application for creating interesting and visually appealing brochures. Here’s how to create (and print out) a tri-fold using PowerPoint. 尽管Microsoft Powe…

彻底理解数据库事物

事务 事务(Transaction)&#xff0c;一般是指要做的或所做的事情。在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。在计算机术语中&#xff0c;事务通常就是指数据库事务。 概念 一个数据库事务通常包含对数据库进行读或写的一个操作序列。它的…