jQuery内容选择器

:contains('john'):表示包含指定字符串的标签,字符串大小写敏感
:empty:表示查询空标签的元素
:has('p'):表示查询有子元素的元素
.addClass("样式名"):为查询到的所有标签添加样式
:parent:表示查询非空标签

`
<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../js/jquery-1.6.js"></script>
<style type="text/css">
.myClass{
font-size:44px;
color:blue
}
</style>
</head>
<body>
<div><p>John Resig</p></div>
<div><p>George Martin</p></div>
<div>Malcom John Sinclair</div>
<div>J. Ohn</div>
<p></p>
<p></p>
<div></div>
<script type="text/javascript">

    //1)查找所有包含文本"John"的div元素的个数
    //alert($("div:contains('john')").size());

    //2)查找所有p元素为空的元素个数
    //alert($("p:empty").size());

    //3)给所有包含p元素的div元素添加一个myClass样式
    //$("div:has('p')").addClass("myClass");

    //4)查找所有含有子元素或者文本的p元素个数,即p为父元素
    alert($("p:parent").size());

</script>

</body>
</html>

`

猜你喜欢

转载自blog.51cto.com/357712148/2113188