jQuery入门(四)--- jQuery语法

it2025-07-26  12

jQuery入门(四)--- jQuery语法

10、获取和设置表单元素的内容 val()11、jQuery操作属性 attr() removeAttr()12、操作布尔类型的属性 prop()13、jQuery尺寸和位置操作13.1 width方法和height方法( 200√ 200px×)13.2 innerWidth / innerHeight / outerWidth / outerHeight 14、offset() 和 position()15、设置或获取页面或元素垂直滚动条的位置scrollTop()与scrollLeft ()16、jQuery事件发展历程17、固定导航栏案例18、jQuery的on注册事件(重点)18.1 on注册简单事件18.2 on注册委托事件 19、事件解绑off方式20、触发器触发 trigger21、事件对象 event / e22、五角星评分案例23、显示迭代 each()24、jQuery多库共存24.1 如何查看jQuery的版本24.2 如果引入多个jQuery文件,使用的$是哪一个jQuery文件中的呢?24.3 多库共存 25、jQuery插件库:jq22.com25.1常用插件25.2 颜色插件 jquery.color.js25.3 省市联动 distpicker插件25.4 jQuery UI插件的使用25.5 自己封装jQuery插件(bgColor)

10、获取和设置表单元素的内容 val()

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <body> <input type="button" value="按钮" id="btn"> <input type="text" id="txt" value="请输入账号"> <script> $(function(){ //原生js是通过value属性来获取或设置表单元素的值 //jQuery中如何设置或者获取表单元素的值呢? val() $('#btn').click(function(){ //1、val()方法 不给参数就是获取表单元素的值 console.log($('#txt').val()); //2、val()方法 给参数就是设置表单元素的值 $('#txt').val('我是设置的值'); }) }) </script> </body>

11、jQuery操作属性 attr() removeAttr()

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <body> <input type="button" value="获取属性" id="btn1"> <input type="button" value="设置属性" id="btn2"> <input type="button" value="移除属性" id="btn3"><br/> <img src="007.jpg" alt="周五晚睡" aaa="aaa"/> <script> $(function(){ //jQuery中操作属性: attr() removeAttr(); //1、设置属性 $('#btn2').click(function(){ //设置单属性 //$('img').attr('src','996.jpg'); //修改以前有的属性 //$('img').attr('aaa','哈哈哈'); //修改自定义属性 //$('img').attr('bbb','bbb'); //添加以前没有的属性 //设置多属性 $('img').attr({ src:'996.jpg', aaa:'hahaha', bbb:'bbb' }); }); //2、获取属性 $('#btn1').click(function(){ console.log($('img').attr('src')); //自带的属性可以获取 console.log($('img').attr('aaa')); //自定义的属性也可以获取 }); //3、移除属性 $('#btn3').click(function(){ //移除单属性 //$('img').removeAttr('alt'); //$('img').removeAttr('aaa'); //$('img').removeAttr('bbb'); //移除多属性 $('img').removeAttr('alt aaa bbb'); }) }); </script> </body>

12、操作布尔类型的属性 prop()

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <body> <input type="button" value="按钮" id="btn1"> <input type="checkbox" id="ckb1"> <script> $(function(){ //原生js操作布尔类型的属性: /* document.getElementById('btn1').onclick = function(){ //设置操作 document.getElementById('ckb1').checked = true; //或false //获取操作 console.log(document.getElementById('ckb1').checked) } */ //jQuery操作布尔类型的属性 $('#btn1').click(function(){ console.log($('#ckb1').prop('checked')); }) }) </script> </body>

13、jQuery尺寸和位置操作

13.1 width方法和height方法( 200√ 200px×)

+ 设置或者获取高度,不包括内边框、边框、外边框、 //带参数表示设置高度 $('img').height(200); //不带参数表示获取高度 $('img').height( ); + 获取网页的可视区高度 //获取可视区宽度 $(window).width(); //获取可视区高度 $(window).height();

13.2 innerWidth / innerHeight / outerWidth / outerHeight

+ innerWidth() / innerHeight() 方法返回元素的宽度 / 高度(包括内边距) + outerWidth() / outerHeight() 方法返回元素的宽度 / 高度(包括内边距和边框) + outerWidth(true) / outerHeight(true) 方法返回元素的宽度 / 高度(包括内边距、边框和外边距)

14、offset() 和 position()

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <style> *{ margin: 0; padding:0; } .father{ width: 400px; height: 400px; border:10px solid red; position: relative; top: 10px; left: 10px; } .son{ width: 100px; height: 100px; border:10px solid green; position:absolute; top:100px; left:100px; } </style> </head> <body> <input type="button" value="offset()" id="btn1"> <input type="button" value="position()" id="btn2"> <div id="father" class="father"> <div id="son" class="son"> </div> </div> <script> $(function(){ //offset() //会得到一个对象,对象里包含 top 和 left $('#btn1').click(function(){ //offset()方法获取元素距离document的位置 console.log($('#son').offset()); //left:120 top:140 }); //position()方法获取元素距离有定位父元素的位置 $('#btn2').click(function(){ console.log($('#son').position()); //left:100 top:100 }) }) </script> </body>

15、设置或获取页面或元素垂直滚动条的位置scrollTop()与scrollLeft ()

//获取页面被卷曲的高度 $(window).scrollTop(); //获取页面被卷曲的宽度 $(window).scrollLeft(); //设置元素被卷曲的高度 $('#div1').scrollTop(100); //设置页面被卷曲的宽度 $('#div1').scrollLeft(100);

16、jQuery事件发展历程

原生js给同一个元素注册不同的事件,后面的会把前面的覆盖掉 jQuery方式给同一个元素注册不同的事件,没有覆盖情况

17、固定导航栏案例

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <style> *{ margin:0; padding:0; } .top{ margin:0 auto; } .nav{ margin:0 auto; } .main{ margin: 10px auto; height: 2000px; } img{ display:block; } </style> </head> <body> <div class="top"> <img src="top.jpg"> </div> <div class="nav"> <img src="nav.jpg"> </div> <div class="main"> <img src="main.jpg"> </div> <script> $(function(){ //思路:给页面设置一个滚动事件,时刻监视页面的scrollTop的值 //如果这个值大于第一部分的高度,设置第二部分为固定定位 //如果这个值小于第一部分的高度,设置第二部分的定位还原 //计算第一部分高度 var topHeight = $('.top').height(); //计算第二部分高度 var navHeight = $('.nav').height(); $(window).scroll(function(){ //计算页面的scrollTop的值 var scrollTopValue = $(window).scrollTop(); //console.log(scrollTopValue); //判断,看scrollTopValue的值是不是大于第一部分的高度 if(scrollTopValue > topHeight){ //大于,让第二部分的定位改为固定定位 $('.nav').css({ position:'fixed', top:0, left:0 }) //设置第三部分的margin-top值为第二部分的高度,不会被第二部分遮挡 $('.main').css({ marginTop:'navHeight' }) }else{ //小于,还原 $('.nav').css({ position:'static', top:0, left:0 }); //设置marginTop的值为原来值 $('.main').css({ marginTop:10 }) } }) }) </script> </body>

18、jQuery的on注册事件(重点)

18.1 on注册简单事件

//表示给$(selector)绑定事件,并且由自己触发,不支持动态绑定 $(selector).on('click',function(){ })

18.2 on注册委托事件

//表示给 $(selector) 绑定代理事件。当必须是他的内部元素div和span才能触发这个事件,支持动态绑定 //这里是selector是div和span的父元素,给父元素绑定事件,指定的子元素可触发该事件,此时this为点击的指定子元素,原理为事件冒泡。 $(selector).on('click' , 'div,span' , function(){ })

19、事件解绑off方式

//解绑匹配元素的所有事件 $(selector).off(); //解绑匹配元素的所有指定的事件(如click事件) $(selector).off('click');

20、触发器触发 trigger

点击按钮触发事件,若要实现达到某条件触发,可用代码的方式来触发事件触发自定义事件 <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <style> .one{ height: 100px; width: 100px; border:1px solid red; } </style> </head> <body> <input type="button" value="按钮1" id="btn1"> <input type="button" value="按钮2" id="btn2"> <div id="one" class="one"></div> <script> $(function(){ //用代码的方式触发事件 //给div注册一个单击事件 $('#one').on('click',function(){ console.log('单击事件'); }) //给按钮注册事件 var i=0; $('#btn1').on('click',function(){ i++; if(i==3){ //条件满足,触发div的单击事件 //事件触发,用代码的方式触发事件 //$('#one').click(); $('#one').trigger('click'); } }) //触发自定义事件 //给div注册一个自定义事件 $('#one').on('linge',function(){ console.log('lalala'); }) //用触发器触发 $('#btn2').on('click',function(){ var res=confirm('请问林哥帅吗'); if(res){ //触发linge()事件 $('#one').trigger('linge'); } }) }) </script> </body>

21、事件对象 event / e

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <style> * { margin:0; padding:0; } body { width: 2000px; height: 2000px; } .one { width: 200px; height: 200px; border: 1px solid red; position:relative; top: 100px; left:100px; } </style> </head> <body> <div id="one" class="one"> <input type="button" value="按钮" id="btn"/> <br/> <a href="https://www.daidu.com">百度一下,你就知道</a> </div> <script> $(function(){ //1、什么是事件对象 //注册一个事件,系统就会帮我们生成一个对象记录这个事件触发时候的一些信息,比如触发事件是有没有按某个键,触发事件是的坐标信息 //jQuery中用事件参数e来获取 //jQuery的事件对象是对原生js事件对象的一个封装,帮你处理好了兼容性 $('#one').on('click',function(e){ //2、事件对象中有常用的三个坐标 console.log('screenx'+':'+e.screenX+'-' +'screeny'+':'+e.screenY); console.log('clientx'+':'+e.clientX+ '-'+'clienty'+':'+e.clientY); console.log('pagex'+':'+e.pageX+ '-'+'pagey'+':'+e.pageY); //3、阻止事件冒泡 alert('我是div的单击事件'); //给按钮注册一个单击事件 $('#btn').on('click',function(e){ alert('我是按钮的单击事件'); e.stopPropagation(); //阻止事件冒泡 }) //4、给a标签注册一个单击事件 $('a').on('click',function(e){ alert('我是a标签的单击事件'); //阻止事件冒泡 e.stopPropagation(); //阻止默认行为 a的跳转 e.preventDefault(); //既能阻止事件冒泡,又能阻止默认行为(上两代码的简写) return false; }) //5、给页面注册键盘按下事件 $(document).on('keydown',function(e){ //e.keyCode能获取按的是哪个键 console.log(e.keyCode); }) }) }) </script> </body>

22、五角星评分案例

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <style> li{ list-style: none; float:left; font-size:30px; } </style> </head> <body> <ul class="comment"> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> <script> $(function(){ //需求1:鼠标移到li标签上,当前li和他之前的li显示实心五角星,后面的li显示空心五角星 //需求2:鼠标离开li标签,所有li都变空心。 //需求3:点击li,鼠标离开后,刚才点击的li和他之前的li显示实心五角星,后面的li显示空心五角星 //prev();上一个兄弟 //prevAll();前面所有兄弟 //next();下一个兄弟 //nextAll(): 之后的所有兄弟 //给li添加事件 $('.comment>li').on('mouseenter',function(){ //需求1 // $(this).text('★').prevAll().text('★').nextAll().text('☆'); //这样不行 //这样可行,加end(),回到上一个状态 //$(this).text('★').prevAll().text('★').end().nextAll().text('☆'); //都是以当前点击的元素为参考确定前面的兄弟和后面的兄弟 $(this).text('★').prevAll().text('★'); $(this).nextAll().text('☆'); }).on('mouseleave',function(){ //需求2 $('.comment>li').text('☆'); //获取刚才点击的li $('.comment>li[clickCurrent]').text('★').prevAll().text('★') ; }).on('click',function(){ //需求3 给鼠标当前点击的li做一个记号 //给当前鼠标点击的这个li添加一个独一无二的属性 $(this).attr('clickCurrent','current').siblings().removeAttr('clickCurrent'); }) }) </script> </body>

23、显示迭代 each()

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <style> li{ list-style:none; display: block; float:left; margin:10px; width: 340px; height: 340px; background-color: pink; } </style> </head> <body> <ul id="ulList"> <li>什么都看不见</li> <li>什么都看不见</li> <li>什么都看不见</li> <li>什么都看不见</li> <li>什么都看不见</li> <li>什么都看不见</li> <li>什么都看不见</li> <li>什么都看不见</li> <li>什么都看不见</li> <li>什么都看不见</li> </ul> <script> $(function(){ //1、获取所有的li标签 var $lis = $('#ulList').children(); //2、遍历jQuery对象集合,为每个匹配的元素执行一个函数 $lis.each(function(index,element){ $(element).css('opacity',(index+1)/10); }) }) </script> </body>

24、jQuery多库共存

24.1 如何查看jQuery的版本

Console.log(jQuery.fn.jquery); Console.log(jQuery.prototype.jquery) Console.log($.fn.jquery) Console.log($.prototype.jquery)

24.2 如果引入多个jQuery文件,使用的$是哪一个jQuery文件中的呢?

console.log($.fn.jquery) 哪个文件后引入,$ 就默认使用哪个文件的

24.3 多库共存

$.noConflict(); //把$符号的控制权给释放了,jquery的版本变为引入的上一个jQuery库的版本 Console.log(jQuery.fn.jquery); Console.log($.fn,jquery);

25、jQuery插件库:jq22.com

25.1常用插件

+ 颜色插件 +省市联动插件 +jQueryUI +查看jQuery插件的源码

25.2 颜色插件 jquery.color.js

//引入jquery <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> //引入颜色插件 <script src="https://cdn.bootcss.com/jquery-color/2.1.2/jquery.color.js"></script> <style> div{ width: 200px; height: 200px; background-color: red; position: relative; top:0; left:0; } </style> <body> <input type="button" value="按钮" id="btn"> <div></div> <script> $(function(){ //需求:点击按钮,让div做动画 $('#btn').on('click',function(){ $('div').animate({ width:100, height:100, left: 800, //animate 动画不能改变背景色,如果要改,就要使用插件(用来做扩展功能) backgroundColor:'green' },2000) }) }) </script> </body>

25.3 省市联动 distpicker插件

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <script src="distpicker.data.js"></script> <script src="distpicker.js"></script> </head> <body> <div id="one"> <select name=""></select> <select name=""></select> <select name=""></select> </div> <script> $(function(){ //使用插件 //引入jQuery文件 //引入插件文件 //调用插件方法 $('#one').distpicker({ province:"福建省", city:"厦门市", district:"湖里区" }); }) </script> </body>

25.4 jQuery UI插件的使用

下载插件(jq22.com)-- 引入插件 -- 复制需要的文件到自己的项目中 -- 在下载的插件文件index.html中查看原代码, 复制框架到自己的项目中并修改---复制调用代码到自己项目中

25.5 自己封装jQuery插件(bgColor)

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <script src="jQuery-bgColor.js"></script> </head> <body> <div></div> <p></p> <script> $(function(){ //1、为啥要封装插件 //$('div').width(100).height(100).css('backgroundColor','red'); //设置背景太麻烦了 //$('div').width(100).height(100).bgColor('red'); //希望这种形式,但目前没有,自己封装 //2、如何自己封装插件 //2.1 给jQuery对象的原型添加方法 //新建jQuery-bgColor.js文件,再引入 $('div').width(100).height(100).bgColor('red'); }) </script> </body> //jQuery-bgColor.js文件中 //自调用函数:可以自己执行代码,传入的参数jQuery用 $来接收 (function($){ //需要给jQuery原型提供方法 $.fn.bgColor = function(yanse){ //this是调用这个方法的jQuery对象 this.css('backgroundColor',yanse) //返回调用这个方法的jQuery本身,这样$('div').bgColor('red')才是个jQuery对象,才可以接着链式编程 return this; } }(jQuery)) //传入jQuery,因为该文件需要给jQuery文件添加方法,需要jQuery
最新回复(0)