分享

jQuery中find函数(查找子元素)和end函数

 以怪力乱神 2018-11-15
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
ul {
list-style-type: none;
width: 200px;
position: absolute;
left: 500px;
}

ul li {
margin-top: 10px;
cursor: pointer;
text-align: center;
font-size: 20px;
}
</style>
<script src="jquery-1.12.1.js"></script>
<script>
$(function () {
$("ul>li").mouseenter(function () {//鼠标进入事件
$(this).css("backgroundColor", "red").siblings("li").css("backgroundColor", "");
}).mouseleave(function () {//鼠标离开事件
//find函数-----寻找子元素中的... 参数可以是任意选择器
$(this).parent().find("li").css("backgroundColor", "");
}).click(function () {//点击事件
// $(this).prevAll("li").css("backgroundColor","yellow");
// $(this).nextAll("li").css("backgroundColor","blue");

//断链:对象调用方法之后,返回的已经不是当前的这个对象了,此时再调用方法,就出现了断链
//.end()方法是修复断链,恢复断链之前的状态,其实就是返回this,也就是指向最前面
//不推荐使用链式编程

$(this).prevAll("li").css("backgroundColor", "yellow").end().nextAll("li").css("backgroundColor", "blue");
});
});
</script>
</head>
<body>
<ul>
<li>青岛啤酒(TsingTao)</li>
<li>瓦伦丁(Wurenbacher)</li>
<li>雪花(SNOW)</li>
<li>奥丁格教士(Franziskaner)</li>
<li>科罗娜喜力柏龙(Paulaner)</li>
<li>嘉士伯Kaiserdom</li>
<li>罗斯福(Rochefort)</li>
<li>粉象(Delirium)</li>
<li>爱士堡(Eichbaum)</li>
<li>哈尔滨牌蓝带</li>
</ul>

</body>
</html>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多