一 jQuery是什么
- jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team。
- jQuery是继prototype之后又一个优秀的Javascript框架。其宗旨是——WRITE LESS,DO MORE!
- 它是轻量级的js库(压缩后只有21k) ,这是其它的js库所不及的,它兼容CSS3,还兼容各种浏览器
- jQuery是一个快速的,简洁的javaScript库,使用户能更方便地处理HTMLdocuments、events、实现动画效果,并且方便地为网站提供AJAX交互。
- jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。
二 jQuery内容
- 选择器
- 筛选器
- 样式操作
- 文本操作
- 属性操作
- 文档处理
- 事件
- 动画效果
- 插件
- each、data、Ajax
下载链接:jQuery官网
三 什么是jQuery对象
jQuery对象就是通过jQuery包装DOM对象后产生的对象。
jQuery对象是jQuery独有的.如果一个对象是jQuery对象,那么它就可以使用jQuery里的方法: $(“#test”).html();
$("#test").html() ?????????//意思是指:获取ID为test的元素内的html代码。其中html()是jQuery里的方法 ???????// 这段代码等同于用DOM实现代码: document.getElementById(" test ").innerHTML; ???????//虽然jQuery对象是包装DOM对象后产生的,但是jQuery无法使用DOM对象的任何方法,同理DOM对象也不能使用jQuery里的方法.乱使用会报错 ???????//约定:如果获取的是 jQuery 对象, 那么要在变量前面加上$. var $variable = jQuery 对象var variable = DOM 对象 $variable[0]:jquery对象转为dom对象 ?????$("#msg").html(); $("#msg")[0].innerHTMLjquery的基础语法:$(selector).action()
详细参考:http://jquery.cuishifeng.cn/
四 寻找元素(选择器和筛选器)
4.1 选择器
4.1.1 基本选择器
1 | $("*") $("#id") $(".class") $("element") $(".class,p,div") |
4.1.2 层级选择器
1 | $(".outer div") $(".outer>div") $(".outer+div") $(".outer~div") |
4.1.3 基本筛选器
1 | $("li:first") $("li:eq(2)") $("li:even") $("li:gt(1)") |
4.1.4属性选择器
1 | $(‘[]‘) $(‘["alex="sb"][id]‘) |
4.1.5 表单选择器
1 | $("[type=‘text‘]")----->$(":text") 注意只适用于input标签 : $("input:checked") |
实例之左侧菜单
View Code
实例之tab切换
View Code
4.2 筛选器
4.2.1过滤筛选器
1 | $("li").eq(2) $("li").first() $("ul li").hasclass("test") |
4.2.2查找筛选器
1 2 3 4 5 6 7 8 9 | $("div").children(".test") $("div").find(".test")$(".test").next() $(".test").nextAll() $(".test").nextUntil()$("div").prev() $("div").prevAll() $("div").prevUntil()$(".test").parent() $(".test").parents() $(".test").parentUntil()$("div").siblings() |
五 操作元素(属性,css,文档处理)
5.1 属性操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | --------------------------属性$("").attr();$("").removeAttr();$("").prop();$("").removeProp();--------------------------CSS类$("").addClass(class|fn)$("").removeClass([class|fn])--------------------------HTML代码/文本/值$("").html([val|fn])$("").text([val|fn])$("").val([val|fn|arr])---------------------------$("").css("color","red") |
注意attr和prop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <inputid="chk1"type="checkbox"/>是否可见<inputid="chk2"type="checkbox"checked="checked"/>是否可见<script>//对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。//对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。//像checkbox,radio和select这样的元素,选中属性对应“checked”和“selected”,这些也属于固有属性,因此//需要使用prop方法去操作才能获得正确的结果。//$("#chk1").attr("checked")//undefined//$("#chk1").prop("checked")//false//---------手动选中的时候attr()获得到没有意义的undefined-----------//$("#chk1").attr("checked")//undefined//$("#chk1").prop("checked")//trueconsole.log($("#chk1").prop("checked"));//falseconsole.log($("#chk2").prop("checked"));//trueconsole.log($("#chk1").attr("checked"));//undefinedconsole.log($("#chk2").attr("checked"));//checked</script> |
实例之全反选
View Code
实例之模态对话框
View Code
5.2 文档处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | //创建一个标签对象$("<p>")//内部插入$("").append(content|fn) ----->$("p").append("<b>Hello</b>");$("").appendTo(content) ----->$("p").appendTo("div");$("").prepend(content|fn) ----->$("p").prepend("<b>Hello</b>");$("").prependTo(content) ----->$("p").prependTo("#foo");//外部插入$("").after(content|fn) ----->$("p").after("<b>Hello</b>");$("").before(content|fn) ----->$("p").before("<b>Hello</b>");$("").insertAfter(content) ----->$("p").insertAfter("#foo");$("").insertBefore(content) ----->$("p").insertBefore("#foo");//替换$("").replaceWith(content|fn) ----->$("p").replaceWith("<b>Paragraph. </b>");//删除$("").empty()$("").remove([expr])知识推荐
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8
不良信息举报平台
互联网安全管理备案
Copyright 2023 www.wodecom.cn All Rights Reserved |