From dd14f55f15910ff48d975d1d15b7ec4b28958b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A8=8A=E5=B0=8F=E9=83=AD?= <2966479092@qq.com> Date: Thu, 14 Dec 2023 12:42:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=8D=81=E5=9B=9B=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...JS\347\232\204\344\275\234\344\270\232.md" | 225 ++++++++++++ ...JS\347\232\204\344\275\234\344\270\232.md" | 343 +++++++----------- 2 files changed, 356 insertions(+), 212 deletions(-) create mode 100644 "47 \346\250\212\345\260\217\351\203\255/20231212 JS\347\232\204\344\275\234\344\270\232.md" diff --git "a/47 \346\250\212\345\260\217\351\203\255/20231212 JS\347\232\204\344\275\234\344\270\232.md" "b/47 \346\250\212\345\260\217\351\203\255/20231212 JS\347\232\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000..bb6eaa0 --- /dev/null +++ "b/47 \346\250\212\345\260\217\351\203\255/20231212 JS\347\232\204\344\275\234\344\270\232.md" @@ -0,0 +1,225 @@ +# jQuery + +jQuery 是一个JavaScript库 + +jQuery极大简化JavaScript编程 + +### jQuery 库特性 + +jQuery 是一个 JavaScript 函数库。 + +jQuery 库包含以下特性: + +- HTML 元素选取 +- HTML 元素操作 +- CSS 操作 +- HTML 事件函数 +- JavaScript 特效和动画 +- HTML DOM 遍历和修改 +- AJAX +- Utilities + +### 添加 jQuery库 + +jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数。 + +可以通过下面的标记把 jQuery 添加到网页中: + +~~~html + + + +~~~ + +请注意, + +``` + +### 使用 Microsoft 的 CDN + +```html + + + +``` + +### 使用百度的CDN + + + +"> + + +## jQuery 语法实例 + +- [$(this).hide()](https://www.w3school.com.cn/tiy/t.asp?f=jquery_hide_this) + + 演示 jQuery hide() 函数,隐藏当前的 HTML 元素。 + +- [$("#test").hide()](https://www.w3school.com.cn/tiy/t.asp?f=jquery_hide_id) + + 演示 jQuery hide() 函数,隐藏 id="test" 的元素。 + +- [$("p").hide()](https://www.w3school.com.cn/tiy/t.asp?f=jquery_hide_p) + + 演示 jQuery hide() 函数,隐藏所有

元素。 + +- [$(".test").hide()](https://www.w3school.com.cn/tiy/t.asp?f=jquery_hide_class) + + 演示 jQuery hide() 函数,隐藏所有 class="test" 的元素。 + +## jQuery 语法 + +jQuery 语法是为 HTML 元素的选取编制的,可以对元素执行某些操作。 + +基础语法是:*$(selector).action()* + +- 美元符号定义 jQuery +- 选择符(selector)“查询”和“查找” HTML 元素 +- jQuery 的 action() 执行对元素的操作 + +### 示例 + +$(this).hide() - 隐藏当前元素 + +$("p").hide() - 隐藏所有段落 + +$(".test").hide() - 隐藏所有 class="test" 的所有元素 + +$("#test").hide() - 隐藏所有 id="test" 的元素 + +**提示:**jQuery 使用的语法是 XPath 与 CSS 选择器语法的组合。在本教程接下来的章节,您将学习到更多有关选择器的语法。 + +关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元素。 + +jQuery 元素选择器和属性选择器允许您通过标签名、属性名或内容对 HTML 元素进行选择。 + +选择器允许您对 HTML 元素组或单个元素进行操作。 + +在 HTML DOM 术语中: + +选择器允许您对 DOM 元素组或单个 DOM 节点进行操作。 + +## jQuery 元素选择器 + +jQuery 使用 CSS 选择器来选取 HTML 元素。 + +$("p") 选取

元素。 + +$("p.intro") 选取所有 class="intro" 的

元素。 + +$("p#demo") 选取所有 id="demo" 的

元素。 + +## jQuery 事件函数 + +jQuery 事件处理方法是 jQuery 中的核心函数。 + +事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。术语由事件“触发”(或“激发”)经常会被使用。 + +通常会把 jQuery 代码放到 部分的事件处理方法中: + +### 实例 + +```html + + + + + + + +

This is a heading

+

This is a paragraph.

+

This is another paragraph.

+ + + + +``` + +## 单独文件中的函数 + +如果您的网站包含许多页面,并且您希望您的 jQuery 函数易于维护,那么请把您的 jQuery 函数放到独立的 .js 文件中。 + +当我们在教程中演示 jQuery 时,会将函数直接添加到 部分中。不过,把它们放到一个单独的文件中会更好,就像这样(通过 src 属性来引用文件): + +### 实例 + +``` + + + + +``` + +## jQuery 名称冲突 + +jQuery 使用 $ 符号作为 jQuery 的简介方式。 + +某些其他 JavaScript 库中的函数(比如 Prototype)同样使用 $ 符号。 + +jQuery 使用名为 noConflict() 的方法来解决该问题。 + +*var jq=jQuery.noConflict()*,帮助您使用自己的名称(比如 jq)来代替 $ 符号。 + +## axios 封装 + +~~~js +/** + * 目标:封装_简易axios函数_获取省份列表_ + * 1. 定义myAxios函数,接收配置对象,返回Promise对象 + * 2. 发起XHR请求,默认请求方法为GET + * 3. 调用成功/失败的处理程序 + * 4. 使用myAxios函数,获取省份列表展示 +*/ +// 1. 定义myAxios函数,接收配置对象,返回Promise对象 +function myAxios(config) { + return new Promise((resolve, reject) => { + // 2. 发起XHR请求,默认请求方法为GET + const xhr = new XMLHttpRequest() + xhr.open(config.method || 'GET', config.url) + xhr.addEventListener('loadend', () => { + // 3. 调用成功/失败的处理程序 + if (xhr.status >= 200 && xhr.status < 300) { + resolve(JSON.parse(xhr.response)) + } else { + reject(new Error(xhr.response)) + } + }) + xhr.send() + }) +} + +// 4. 使用myAxios函数,获取省份列表展示 +myAxios({ + url: 'http://hmajax.itheima.net/api/province' +}).then(result => { + console.log(result) + document.querySelector('.my-p').innerHTML = result.list.join('
') +}).catch(error => { + console.log(error) + document.querySelector('.my-p').innerHTML = error.message +}) +~~~ \ No newline at end of file diff --git "a/47 \346\250\212\345\260\217\351\203\255/20231213 JS\347\232\204\344\275\234\344\270\232.md" "b/47 \346\250\212\345\260\217\351\203\255/20231213 JS\347\232\204\344\275\234\344\270\232.md" index bb6eaa0..1312d54 100644 --- "a/47 \346\250\212\345\260\217\351\203\255/20231213 JS\347\232\204\344\275\234\344\270\232.md" +++ "b/47 \346\250\212\345\260\217\351\203\255/20231213 JS\347\232\204\344\275\234\344\270\232.md" @@ -1,225 +1,144 @@ -# jQuery +# 作业 -jQuery 是一个JavaScript库 - -jQuery极大简化JavaScript编程 - -### jQuery 库特性 - -jQuery 是一个 JavaScript 函数库。 - -jQuery 库包含以下特性: - -- HTML 元素选取 -- HTML 元素操作 -- CSS 操作 -- HTML 事件函数 -- JavaScript 特效和动画 -- HTML DOM 遍历和修改 -- AJAX -- Utilities - -### 添加 jQuery库 - -jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数。 - -可以通过下面的标记把 jQuery 添加到网页中: - -~~~html - - - -~~~ - -请注意, - -``` - -### 使用 Microsoft 的 CDN +tab栏切换 ```html - - - -``` - -### 使用百度的CDN - - - -"> - - -## jQuery 语法实例 - -- [$(this).hide()](https://www.w3school.com.cn/tiy/t.asp?f=jquery_hide_this) - - 演示 jQuery hide() 函数,隐藏当前的 HTML 元素。 - -- [$("#test").hide()](https://www.w3school.com.cn/tiy/t.asp?f=jquery_hide_id) - - 演示 jQuery hide() 函数,隐藏 id="test" 的元素。 - -- [$("p").hide()](https://www.w3school.com.cn/tiy/t.asp?f=jquery_hide_p) - - 演示 jQuery hide() 函数,隐藏所有

元素。 - -- [$(".test").hide()](https://www.w3school.com.cn/tiy/t.asp?f=jquery_hide_class) - - 演示 jQuery hide() 函数,隐藏所有 class="test" 的元素。 - -## jQuery 语法 - -jQuery 语法是为 HTML 元素的选取编制的,可以对元素执行某些操作。 - -基础语法是:*$(selector).action()* - -- 美元符号定义 jQuery -- 选择符(selector)“查询”和“查找” HTML 元素 -- jQuery 的 action() 执行对元素的操作 - -### 示例 - -$(this).hide() - 隐藏当前元素 - -$("p").hide() - 隐藏所有段落 - -$(".test").hide() - 隐藏所有 class="test" 的所有元素 - -$("#test").hide() - 隐藏所有 id="test" 的元素 - -**提示:**jQuery 使用的语法是 XPath 与 CSS 选择器语法的组合。在本教程接下来的章节,您将学习到更多有关选择器的语法。 - -关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元素。 - -jQuery 元素选择器和属性选择器允许您通过标签名、属性名或内容对 HTML 元素进行选择。 - -选择器允许您对 HTML 元素组或单个元素进行操作。 - -在 HTML DOM 术语中: - -选择器允许您对 DOM 元素组或单个 DOM 节点进行操作。 - -## jQuery 元素选择器 - -jQuery 使用 CSS 选择器来选取 HTML 元素。 - -$("p") 选取

元素。 - -$("p.intro") 选取所有 class="intro" 的

元素。 - -$("p#demo") 选取所有 id="demo" 的

元素。 - -## jQuery 事件函数 - -jQuery 事件处理方法是 jQuery 中的核心函数。 - -事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。术语由事件“触发”(或“激发”)经常会被使用。 - -通常会把 jQuery 代码放到 部分的事件处理方法中: - -### 实例 - -```html - - - - + - - -

This is a heading

-

This is a paragraph.

-

This is another paragraph.

- + //获取所有最上面的标题(精选、美食、、百货、个护、预告),‘ ’空格代表下一级 + let a = document.querySelectorAll('.tab-nav a'); +// console.log($('.tab-nav a')); + // console.log(a);效果如下 + // NodeList(5) [a.active, a, a, a, a] + // 0: a.active + // 1: a + // 2: a + // 3: a + // 4: a + // length: 5 + // [[Prototype]]: NodeList + + //获取所有的照片 + let img = document.querySelectorAll('.tab-content .item'); + // console.log(img);效果如下 + // NodeList(5) [div.item.active, div.item, div.item, div.item, div.item] + // 0: div.item.active + // 1: div.item + // 2: div.item + // 3: div.item + // 4: div.item + // length: 5 + // [[Prototype]]: NodeList + + + for(let i=0;i - - ``` -## 单独文件中的函数 +轮播图 -如果您的网站包含许多页面,并且您希望您的 jQuery 函数易于维护,那么请把您的 jQuery 函数放到独立的 .js 文件中。 - -当我们在教程中演示 jQuery 时,会将函数直接添加到 部分中。不过,把它们放到一个单独的文件中会更好,就像这样(通过 src 属性来引用文件): - -### 实例 +```html + + - - -``` -## jQuery 名称冲突 - -jQuery 使用 $ 符号作为 jQuery 的简介方式。 - -某些其他 JavaScript 库中的函数(比如 Prototype)同样使用 $ 符号。 - -jQuery 使用名为 noConflict() 的方法来解决该问题。 - -*var jq=jQuery.noConflict()*,帮助您使用自己的名称(比如 jq)来代替 $ 符号。 - -## axios 封装 - -~~~js -/** - * 目标:封装_简易axios函数_获取省份列表_ - * 1. 定义myAxios函数,接收配置对象,返回Promise对象 - * 2. 发起XHR请求,默认请求方法为GET - * 3. 调用成功/失败的处理程序 - * 4. 使用myAxios函数,获取省份列表展示 -*/ -// 1. 定义myAxios函数,接收配置对象,返回Promise对象 -function myAxios(config) { - return new Promise((resolve, reject) => { - // 2. 发起XHR请求,默认请求方法为GET - const xhr = new XMLHttpRequest() - xhr.open(config.method || 'GET', config.url) - xhr.addEventListener('loadend', () => { - // 3. 调用成功/失败的处理程序 - if (xhr.status >= 200 && xhr.status < 300) { - resolve(JSON.parse(xhr.response)) - } else { - reject(new Error(xhr.response)) - } +// 1. 初始数据 +const data = [ + { url: './images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' }, + { url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' }, + { url: './images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' }, + { url: './images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' }, + { url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' }, + { url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' }, + { url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' }, + { url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' }, + ] + // 获取元素 + let img=document.querySelector('.slider-wrapper img') + let p=document.querySelector('.slider-footer p') + let footer=document.querySelector('.slider-footer') + // let prebtn=document.querySelector('.toggle .prev') + // let nextbtn=document.querySelector('.toggle .next') + + + + // 定义一个全局的图片位置的变量 + let num=0 + + + // 先用定时器让其自动转换 + let timer= setInterval(function(){ + // nextbtn.click(); + $('.toggle .next')[0].click(); + },1000) + + + // 鼠标移动到图片,就暂时滚动 + img.addEventListener('mouseenter',function(){ + clearInterval(timer) }) - xhr.send() - }) -} - -// 4. 使用myAxios函数,获取省份列表展示 -myAxios({ - url: 'http://hmajax.itheima.net/api/province' -}).then(result => { - console.log(result) - document.querySelector('.my-p').innerHTML = result.list.join('
') -}).catch(error => { - console.log(error) - document.querySelector('.my-p').innerHTML = error.message + img.addEventListener('mouseleave',function(){ + timer= setInterval(function(){ + $('.toggle .next')[0].click(); + },1000) + }) + + // 下一页 + $('.toggle .next').click(function(){ + num++; + if(num>=data.length){ + num=0} + img.src=data[num].url + p.innerHTML=data[num].title + footer.style.backgroundColor =data[num].color + // document.querySelector('.slider-indicator .active').classList.remove('active') + $('.slider-indicator .active')[0].classList.remove('active') + // document.querySelector(`.slider-indicator li:nth-child(${num+1})`).classList.add('active') + $(`.slider-indicator li:nth-child(${num+1})`)[0].classList.add('active') + }) + + + // 上一页 + $('.toggle .prev').click(function(){ + num--; + if(num<0){ + num=data.length} + img.src=data[num].url + p.innerHTML=data[num].title + footer.style.backgroundColor =data[num].color + // document.querySelector('.slider-indicator .active').classList.remove('active') + $('.slider-indicator .active')[0].removeClass('active') + // document.querySelector(`.slider-indicator li:nth-child(${num+1})`).classList.add('active') + $(`.slider-indicator li:nth-child(${num+1})`)[0].addClass('active') + }) }) -~~~ \ No newline at end of file + +``` \ No newline at end of file -- Gitee