选择某id的父节点元素
使用 jQuery parents() 方法,参考链接:http://www.w3school.com.cn/jquery/jquery_traversing_ancestors.asp
如下实例中为选择id为cus的父节点中的第一个div元素
$("#cus").parents("div:first").css({"color":"red","border":"2px solid red"});
<!DOCTYPE html>
<html>
<head>
<style>
.ancestors *
{
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("#cus").parents("div:first").css({"color":"red","border":"2px solid red"});
});
</script>
</head>
<body class="ancestors">body (曾曾祖父)
<div style="width:500px;">div (曾祖父)
<div style="width:45000px;">(custom)
<ul>ul (祖父)
<li>li (直接父)
<span id="cus" >span</span>
</li>
</ul>
</div>
</div>
</body>
<!-- 最外围的红色边框,在 body 元素之前,是 html 元素(也是祖先)。 -->
</html>
结果图: