1.jQuery是什么?
jQuery就是javascript的一个库,把我们常用的一些功能进行了封装,方便我们来调用,提高我们的开发效率。
极大地简化了 JavaScript 编程。
Javascipt跟jQuery的区别:
Javascript是一门编程语言,我们用它来编写客户端浏览器脚本。
jQuery是javascript的一个库,包含多个可重用的函数,用来辅助我们简化javascript开发
jQuery能做的javascipt都能做到,而javascript能做的事情,jQuery不一定能做到。
2.jQuery的入门
想快速的入门jQuery,首先我们就需要知道它的一个重点。。。写的不是很好,如果你是有一定的基础那我推荐你去看 jQuery的 在线文档,或者去菜鸟教程
在线文档:https://www.94xh.com/
菜鸟教程:https://www.runoob.com/jquery/jquery-tutorial.html
2.1,jQuery 选择器
/* //利用a标签获取jQuery实例
$("a").click(function(){
alert("aaaa");
}) */
//ID选择器
/* $("#a3").click(function(){
alert("bbb");
}) */
//类选择器
/* $(".c1").click(function(){
alert("ccc");
}) */
//包含选择器
/* $("p a").click(function(){
alert("ccc");
}) */
//组合选择器
/* $("a,span").click(function(){
alert("ccc");
}) */
//div里面的a标签加上事件,在div标签内部寻找a标签
//如果是第二个参数没有填,默认是document,body里面
$("a","div").click(function(){
alert("dddd")
})
2.2,jQuery的 事件
jQuery的事件有很多,我就截取了一小部分,想知道更加详细的可以去api文档里看
2.3,js对象转jQuery对象
var h2Node = document.getElementById("h2")
alert(h2Node.value);
//js对象转jQuery对象
var $h2Node = $(h2Node);
alert($h2Node.val());
2.4,jQuery对象转js对象
var $h1 = $("#h1");
alert($h1.val());
//jQuery对象转js对象,集合
var h1Node = $h1.get(0);
alert(h1Node.value);
//jQuery对象转js对象,数组
var h1Node = $h1[0];
alert(h1Node.value);
2.5,this的作用
$(function(){
$(":input").click(function(){
//this事件源
alert(this.value)
$("a").each(function(index,itm){
//this指当前元素,index是下标
alert(index+","+$(this).html())
})
})
})
3.自定义jQuery插件
要清楚自定义jQuery的插件首先需要清楚jQuery的类方法类属性,和实例方法。好滴话不多说,直接上代码!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.min.js"></script><title>Insert title here</title>
<title>Insert title here</title>
<script type="text/javascript">
$(function