Jquery搜索父元素操作方法?
使用js或者jquery查找父元素、子元素经常遇到。可是用起来总容易混淆,这里统一总结了一下,以后用起来相信会方便好多
这里jquery向上查找父元素 用到的方法:
closest() parents() parent()
向下查找子元素
用到的方法:find() children()
js用的是 children[] 属性 !
jQuery给多个不同元素添加class样式的方法?
jQuery可以通过addClass()方法给多个不同的html元素同时添加相同的class
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").addClass("blue");
$("div").addClass("important");
});
});
</script>
<style type="text/css">
.important
{
font-weight:bold;
font-size:xx-large;
}
.blue
{
color:blue;
}
</style>
</head>
<body>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<div>This is some important text!</div>
<br>
<button>Add classes to elements</button>
</body>
</html>
JQUERY如何获得某元素父级的父级?
parent()是 父元素 就一个,你要选取class=x的元素,就是第一个p元素的父元素的父元素:$("p:eq(0)").parent().parent().css("background", "yellow");或者p元素父元素的父元素类为x的元素:$("p").parent().parent(".x").css("background", "yellow");或者p元素祖先元素(祖先就很多个了)中类为x的元素: $("p").parents(".x").css("background", "yellow");

