Skip to content

jQuery 笔记

我们可以进入jquery.com下载jQuery文件,如jquery-3.7.1.min.js
在HTML文件中引入jQuery,放在自己写的js文件前面:

<html>
<head>
<title>jQuery</title>
<script src="./js/jquery-3.7.1.min.js"></script>
</head>
<body>
<div id="a">
<p>abc</p>
<p>
<span id="span1">span1</span>
<span>span2</span>
<span>span3</span>
</p>
</div>
<div id="b"></div>
<div class="c">a</div>
<div class="c">b</div>
<div class="c">c</div>
<script src="./js/index.js"></script>
</body>
</html>

两种jQuery的准备方法,后面可以在花括号内编写代码,代码将会在文档加载后执行:

// 方法1
$(document).ready(function(){
})
// 方法2
$(function(){
})

click添加点击事件,例如点击id=“a”的元素时弹出警告框:

$(document).ready(function(){
$("#a").click(function(){
alert(123)
})
})

在jQuery中可以使用CSS选择器来选取元素,#开头代表id,.开头代表class,用:eq()选择下标,例如:

//选取id="a"的元素
$("#a")
//选取class="c"的第1个元素(下标为0)
$(".c:eq(0)")
//选取第5个<div>元素(下标为4)
$("div:eq(4)")
//选取id="a"的<div>元素内的第1个<p>元素
$("div#a p:eq(0)")
//选取id="a"的元素内的第2个<p>元素内的第3个<span>元素
$("#a p:eq(1) span:eq(2)")

弹出所选元素的内容:alert($("#a").html())

mouseenter添加鼠标进入事件,mouseleave添加鼠标移出事件。
可以用hover添加悬停事件,传入2个函数,对应鼠标进入和移出,例如:

$("#span1").hover(
function(){
$(this).html("123")
},
function(){
$(this).html("abc")
}
)

检查表单提交的值:

<form id="f" action="https://www.baidu.com">
<input type="text" id="name">
<input type="submit" value="提交">
</form>
$("#f").submit(function(){
if($("#name").val() == "baidu"){
alert("是baidu")
return true
} else {
alert("不是baidu")
return false
}
})

在准备方法之外定义函数,用不带参数的submit触发submit事件:

function c(){
alert("触发submit事件")
$("#f").submit()
}

hide隐藏,show显示:

<p id="a">abc</p>
<button id="h">hide</button>
<button id="s">show</button>
$("#h").click(function(){
$("#a").hide()
})
$("#s").click(function(){
$("#a").show()
})

toggle切换显示和隐藏:

<button id="t">toggle</button>
$("#t").click(function(){
$("#a").toggle()
})

给隐藏和显示增加执行时间和回调函数

Section titled “给隐藏和显示增加执行时间和回调函数”

给隐藏和显示增加执行时间和回调函数:

$("#h").click(function(){
$("#a").hide(3000, function(){
$("#s").click()
})
})
$("#s").click(function(){
$("#a").show(1000)
})

fadeIn淡入,fadeOut淡出,fadeToggle淡入淡出切换:

$("#h").click(function(){
$("#a").fadeOut(3000, function(){
$("#s").click()
})
})
$("#s").click(function(){
$("#a").fadeIn(1000)
})
$("#t").click(function(){
$("#a").fadeToggle()
})

slideDown划入,slideUp划出,slideToggle划入划出切换:

$("#h").click(function(){
$("#a").slideUp(3000, function(){
$("#s").click()
})
})
$("#s").click(function(){
$("#a").slideDown(1000)
})
$("#t").click(function(){
$("#a").slideToggle()
})

lefttop属性设置移动后的位置:

<div style="background-color:#66ccff;width:200px;height:200px;position:relative;top:10px;right:10px" id="a">
abc
</div>
function move(){
$("#a").animate({
left:"200px",
top:"300px",
})
}

修改动画时间为4秒:

function move(){
$("#a").animate({
left:"+=200px",
top:"+=300px",
}, 4000)
}

fontSize属性修改字体大小,增加30px:

function move(){
$("#a").animate({
left:"+=200px",
top:"+=300px",
fontSize:"+=30px",
}, 4000)
}

widthheight属性修改宽高,减少60px:

function move(){
$("#a").animate({
left:"+=200px",
top:"+=300px",
width:"-=60px",
height:"-=60px",
}, 4000)
}

一天学会JQuery