jquery内容选择器(匹配包含指定选择器的元素)

时间:2022-12-18 14:36:46
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>内容选择器</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<style type="text/css">
.yang
{ border-collapse: collapse; width:500px;height:30px;border:1px solid red;"}
</style>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
/*
:contains(text) //根据内容匹配元素
:empty //匹配内容为空的元素
:has(selector) //匹配包含指定选择器的元素
:parent //匹配内容不为空的元素
*/
window.onload
=function(){
$(
'#btnOk').click(function(){
//匹配内容中包含a选择器的所有li
$("li:has(a)").html('水浒传');
});
};
</script>
</head>
<body>

<ol>
<li>三国演义</li>
<li><a>西游记</a></li>
<li></li>
<li>红楼梦</li>
</ol>

<hr/>
<input type="button" id='btnOk' value='确定' />
</body>
</html>