選擇器

// 什么是 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.pswp.cn/news/278614.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278614.shtml
英文地址,請注明出處:http://en.pswp.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;事務通常就是指數據庫事務。 概念 一個數據庫事務通常包含對數據庫進行讀或寫的一個操作序列。它的…