day56--jQuery(初识)

jQuery

jQuery介绍

"""
(1)jQuery是一个轻量级的、兼容多浏览器的JavaScript库;
(2)jQuery使用户能够更方便地处理HTML Document、Events、实现动画效果、方便地进行Ajax交互,能够极大地简化JavaScript编程。它的宗旨就是:“Write less, do more.“
"""

jQuery的优势

"""
(1)一款轻量级的JS框架。jQuery核心js文件才几十kb,不会影响页面加载速度;
(2)丰富的DOM选择器,jQuery的选择器用起来很方便,比如要找到某个DOM对象的相邻元素,JS可能要写好几行代码,而jQuery一行代码就搞定了,再比如要将一个表格的隔行变色,jQuery也是一行代码搞定;
(3)链式表达式。jQuery的链式操作可以把多个操作写在一行代码里,更加简洁;
(4)事件、样式、动画支持。jQuery还简化了js操作css的代码,并且代码的可读性也比js要强;
(5)Ajax操作支持。jQuery简化了AJAX操作,后端只需返回一个JSON格式的字符串就能完成与前端的通信;
(6)跨浏览器兼容。jQuery基本兼容了现在主流的浏览器,不用再为浏览器的兼容问题而伤透脑筋;
(7)插件扩展开发。jQuery有着丰富的第三方的插件,例如:树形菜单、日期控件、图片切换插件、弹出窗口等等基本前端页面上的组件都有对应插件,并且用jQuery插件做出来的效果很炫,并且可以根据自己需要去改写和封装插件,简单实用。
"""

jQuery的导入方式

文件导入
# 下载jQuery文件(jquery-3.5.1.min.js)到本地
官网地址:https://jquery.com/download/
通过CDN导入
# CDN(Content Delivery Network):内容分发网络
# 前端免费的CDN:https://www.bootcdn.cn/
# jQuery中文文档:http://jquery.cuishifeng.cn/

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

jQuery基本语法

jQuery(selector).action()
简写:
$(selector).action()

jQuery与js代码对比

# 将p标签的背景色改为红色
js代码
<script>
    let pEle = window.document.getElementById('d1');
    pEle.style.color = "red";
</script>
jQuery代码:
<script>
    $('#d1').css('color','red');
</script>

jquery对象

jQuery对象就是通过jQuery包装DOM对象后产生的对象。jQuery对象是 jQuery独有的。如果一个对象是 jQuery对象,那么它就可以使用jQuery里的方法:例如$(“#i1”).html()。

$("#i1").html()的意思是:获取id值为 i1的元素的html代码。其中 html()是jQuery里的方法。

相当于: document.getElementById("i1").innerHTML;

虽然 jQuery对象是包装 DOM对象后产生的,但是 jQuery对象无法使用 DOM对象的任何方法,同理 DOM对象也没不能使用 jQuery里的方法。

一个约定,我们在声明一个jQuery对象变量的时候在变量名前面加上$:

var $variable = jQuery对像
var variable = DOM对象
$variable[0]//jQuery对象转成DOM对象

拿上面那个例子举例,jQuery对象和DOM对象的使用:

$("#i1").html();//jQuery对象可以使用jQuery的方法
$("#i1")[0].innerHTML;// DOM对象使用DOM的方法

jQuery查找标签

基本选择器

id选择器

$('#d1')

class选择器

$('.c1')

标签选择器

$('span')

jQuery对象与标签对象的相互转换

# jQuery对象转换成标签对象
$('#d1')[0]
<p id=​"d1" style=​"color:​ red;​">​O(∩_∩)O哈哈~​</p>​
# 标签对象转换为jQuery对象
let pEle = window.document.getElementById('d1')

$(pEle)
S.fn.init [p#d1]
组合选择器(分组/嵌套)
$('div')
w.fn.init(2) [div#d1, div.c1, prevObject: w.fn.init(1)]
$('div.c1')
w.fn.init [div.c1, prevObject: w.fn.init(1)]0: div.c1length: 1prevObject: w.fn.init [document]__proto__: Object(0)
$('div#d1')
w.fn.init [div#d1, prevObject: w.fn.init(1)]
$('*')
w.fn.init(19) [html, head, meta, title, meta, link, script, script, body, span, span, div#d1, span, p#d2, span, span, div.c1, span, span, prevObject: w.fn.init(1)]
               
$('#d1,.c1,p')  # 并列+混用
w.fn.init(3) [div#d1, p#d2, div.c1, prevObject: w.fn.init(1)]
              
$('div span')  # 后代
w.fn.init(3) [span, span, span, prevObject: w.fn.init(1)]
$('div>span')  # 儿子
w.fn.init(2) [span, span, prevObject: w.fn.init(1)]
$('div+span')  # 毗邻
w.fn.init [span, prevObject: w.fn.init(1)]
$('div~span')  # 弟弟
w.fn.init(2) [span, span, prevObject: w.fn.init(1)]
基本筛选器
$('ul li')
w.fn.init(10) [li, li, li, li, li, li, li.c1, li, li#d1, li, prevObject: w.fn.init(1)]
               
$('ul li:first')  # 大儿子 
w.fn.init [li, prevObject: w.fn.init(1)]0: lilength: 1prevObject: w.fn.init [document]__proto__: Object(0)
               
$('ul li:last')  # 小儿子
w.fn.init [li, prevObject: w.fn.init(1)]0: lilength: 1prevObject: w.fn.init [document]__proto__: Object(0)
               
$('ul li:eq(2)')		# 放索引
w.fn.init [li, prevObject: w.fn.init(1)]0: lilength: 1prevObject: w.fn.init [document]__proto__: Object(0)
               
$('ul li:even')  # 偶数索引 0包含在内
w.fn.init(5) [li, li, li, li.c1, li#d1, prevObject: w.fn.init(1)]0: li1: li2: li3: li.c14: li#d1length: 5prevObject: w.fn.init [document]__proto__: Object(0)
              
$('ul li:odd')  # 奇数索引
w.fn.init(5) [li, li, li, li, li, prevObject: w.fn.init(1)]0: li1: li2: li3: li4: lilength: 5prevObject: w.fn.init [document]__proto__: Object(0)
              
$('ul li:gt(2)')  # 大于索引
w.fn.init(7) [li, li, li, li.c1, li, li#d1, li, prevObject: w.fn.init(1)]0: li1: li2: li3: li.c14: li5: li#d16: lilength: 7prevObject: w.fn.init [document]__proto__: Object(0)
              
$('ul li:lt(2)')  # 小于索引
w.fn.init(2) [li, li, prevObject: w.fn.init(1)]0: li1: lilength: 2prevObject: w.fn.init [document]__proto__: Object(0)
              
$('ul li:not("#d1")')  # 移除不满满足条件的标签
w.fn.init(9) [li, li, li, li, li, li, li.c1, li, li, prevObject: w.fn.init(1)]
         
$('div')
w.fn.init(2) [div, div, prevObject: w.fn.init(1)]
$('div:has("p")')  # 选取出包含一个或多个标签在内的标签
w.fn.init [div, prevObject: w.fn.init(1)]
属性选择器
$('[username]')
w.fn.init(3) [input, input, p, prevObject: w.fn.init(1)]
$('[username="jason"]')
w.fn.init [input, prevObject: w.fn.init(1)]
$('p[username="egon"]')
w.fn.init [p, prevObject: w.fn.init(1)]

$('[type]')
w.fn.init(2) [input, input, prevObject: w.fn.init(1)]
$('[type="text"]')
w.fn.init(2) [input, input, prevObject: w.fn.init(1)]
表单筛选器
$('input[type="text"]')
w.fn.init [input, prevObject: w.fn.init(1)]0: inputlength: 1prevObject: w.fn.init [document]__proto__: Object(0)
$('input[type="password"]')
w.fn.init [input, prevObject: w.fn.init(1)]

$(':text')  # 等价于上面第一个
w.fn.init [input, prevObject: w.fn.init(1)]0: inputlength: 1prevObject: w.fn.init [document]__proto__: Object(0)
$(':password')  # 等价于上面第二个
w.fn.init [input, prevObject: w.fn.init(1)]

/*
:text
:password
:file
:radio
:checkbox
:submit
:reset
:button
...
*/

表单对象属性
:enabled
:disabled
:checked
:selected
"""特殊情况"""
$(':checked')  # 它会将checked和selected都拿到
w.fn.init(2) [input, option, prevObject: w.fn.init(1)]0: input1: optionlength: 2prevObject: w.fn.init [document]__proto__: Object(0)
$(':selected')  # 它不会 只拿selected
w.fn.init [option, prevObject: w.fn.init(1)]

$('input:checked')  # 自己加一个限制条件
w.fn.init [input, prevObject: w.fn.init(1)]
筛选器方法
$('#d1').next()  // 获取同级别下一个标签

$('#d1').nextAll() // 获取同级别下方的所有标签

$('#d1').nextUntil('.c1')  //获取从当前标签开始到指定标签之间的所有同级标签(不包括指定标签)

              
              
$('.c1').prev()   //获取同一级别的上一个标签

$('.c1').prevAll()

$('.c1').prevUntil('#d2')

              
$('#d3').parent()  // 第一级父标签

$('#d3').parent().parent()

$('#d3').parents()  // 获取所有父标签

$('#d3').parentsUntil('body')  //到body父标签之前的所有父标签(不包括body)
                            
$('#d2').children()  //获取当前的子标签
              
$('#d2').siblings()  // 同级别上下所有兄弟标签
              
              
              
$('div p')
# 等价           
$('div').find('p')  // find从某个区域内筛选出想要的标签 
              
              
"""下述两两等价"""
$('div span:first')  //筛选出第一个指定标签

$('div span').first()                                                                                  
$('div span:last')

$('div span').last()
                                                                                    
$('div span:not("#d3")')

$('div span').not('#d3')  //筛选出不满足条件的所有指定标签

原文地址:https://www.cnblogs.com/surpass123/p/12918448.html