<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test3</title>
<script src="js/jquery-1.6.js"></script>
<style type="text/css">
body {
color: lightseagreen;
text-align: center;
}
<style type="text/css">.myClass {
font-size: 44px;
color: blue
}
</style>
</style>
</head>
<body>
<hr>
<div id="div1">div1</div>
<div id="div2">div2</div>
<div class="divClass">divClass</div>
<p id="pid"></p>
<hr>
<form>
<input type="text" value="a" />
<table>
<tr>
<td><input type="checkbox" value="b" /></td>
</tr>
</table>
</form>
<input type="radio" value="c" />
<input type="radio" value="d" />
<input type="radio" value="e" />
<hr>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
<input type="checkbox" checked/>
<input type="checkbox" checked/>
<input type="checkbox" />
<table border="1">
<tr>
<td>line1</td>
</tr>
<tr>
<td>line2</td>
</tr>
<tr>
<td>line3</td>
</tr>
</table>
<h1>h1</h1>
<h2>h2</h2>
<h3>h3</h3>
<p>p</p>
<hr />
<div>
<p>John Resig</p>
</div>
<div>
<p>George Martin</p>
</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn</div>
<div></div>
<p></p>
<p></p>
<hr />
<table border="1" align="center">
<tr style="display:none">
<td>Value 1</td>
</tr>
<tr>
<td>Value 2</td>
</tr>
<tr>
<td>Value 3</td>
</tr>
</table>
<hr />
<div>
<p>Hello!</p>
</div>
<div id="test2"></div>
<input type="checkbox" name="newsletter" value="Hot Fuzz" />
<input id="myID" type="checkbox" name="newsletter" value="Cold Fusion" />
<input type="checkbox" name="newsaccept" value="Evil Plans" />
<hr />
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
</ul>
<ul>
<li>Marry</li>
</ul>
<ul>
<li>Jack</li>
</ul>
<hr />
</body>
<script type="text/javascript">
window.document.write("<br>jQuery选择器:<br>");
window.document.write($("#div1").size() + " , ");
window.document.write($("#div2").size() + " , ");
window.document.write($(".divClass").length + " , ");
window.document.write($("div,p").size() + " , ");
window.document.write($("#div1,#div2,.divClass,p").size() + "<br>");
window.document.write(" <br>");
window.document.write("找到表单form里所有的input元素的个数:2," + $("form input").size() + "<br>");
window.document.write("找到表单form里所有的子级input元素个数:1, " + $("form>input").size() + "<br>");
window.document.write("找到表单form同级第一个input元素的value属性值:c," + $("form+input").val() + "<br>");
window.document.write("找到所有与表单form同级的input元素个数:3," + $("form~input").size() + "<br>");
window.document.write(" <br>");
window.document.write("查找UL中第一个li元素的内容:list item 1," + $("ul li:first").text() + " <br>");
window.document.write("查找UL中最后个元素的内容:list item 5," + $("ul li:last").text() + " <br>");
window.document.write("查找表格的索引号为1、3、5...奇数行个数,索引号从0开始," + $("table tr:odd").size() + " <br>");
window.document.write("查找表格的索引号为2、4、6...偶数行个数,索引号从0开始," + $("table tr:even").size() + " <br>");
window.document.write("查找表格中第二行的内容,从索引号0开始,这是一种祖先 后代 的变化形式,line2," + $("table tr:eq(2)").text() + " <br>");
window.document.write("查找表格中第二第三行的个数,即索引值是1和2,也就是比0大,3," + $("table tr:gt(0)").size() + "<br>");
window.document.write("查找表格中第一第二行的个数,即索引值是0和1,也就是比2小,2," + $("table tr:lt(2)").size() + "<br>");
//给页面内所有标题<h1><h2><h3>加上红色背景色,且文字加蓝色
$(":header").css("background-color", "red").css("color", "#ffff33");
//查找所有[未]选中的input为checkbox的元素个数
window.document.write("$(':checkbox:not(:checked)').size(),2" + $(":checkbox:not(:checked)").size() + "<br>");
window.document.write(" <br>");
window.document.write("查找所有包含文本John的div元素的个数:$(div:contains('jonin')).size():" + $("div:contains('jonin')").size() + "<br>");
window.document.write("查找所有p元素为空的元素个数:$('p:empty'').size():" + $("p:empty").size() + "<br>");
window.document.write("给所有包含p元素的div元素添加一个myClass样式: " + $("div:has(p)").addClass("myClass") + "<br>");
window.document.write("查找所有含有子元素或者文本的p元素个数,即p为父元素:$('p:parent').size():" + $("p:parent").size() + "<br>");
window.document.write(" <br>");
window.document.write("查找隐藏的tr元素的个数:$('table tr:hidden').size():" + $("table tr:hidden").size() + "<br>");
window.document.write("查找所有可见的tr元素的个数:$('table tr:vsisible').size():" + $("table tr:visible").size() + "<br>");
window.document.write(" <br>");
window.document.write("查找所有含有id属性的div元素个数:" + $('div[id]').size() + "<br>");
window.document.write("查找所有name属性是newsletter的input元素,并将其选中:" + "<br>");
$("input[name='newsletter']").attr("checked", "checked");
window.document.write("查找所有name属性不是newsletter的input元素,并将其选中:" + "<br>");
$("input[name!='newsletter']").attr("checked", "true");
window.document.write(" <br>");
window.document.write("查找所有name属性以'news'开头的input元素,并将其选中");
$("input[name^='news']").attr("checked", "checked");
window.document.write("查找所有name属性以'letter'结尾的input元素,并将其选中");
$("input[name$='letter']").attr("checked", "checked");
window.document.write("查找所有name属性包含'news'的input元素,并将其选中");
$("input[name*='news']").attr("checked", "checked");
window.document.write("找到所有含有id属性,并且它的name属性是以letter结尾的input元素,并将其选中");
$("input[id][name$='letter']").attr("checked", "true");
window.document.write(" <br>");
window.document.write("迭代[each]每个ul中第1个li元素中的内容,索引从1开始"+ "<br>");
/*
$("ul li:first-child").each(function(){
alert( $(this).text() );
});
*/
window.document.write("迭代每个ul中最后1个li元素中的内容,索引从1开始"+ "<br>");
/*
$("ul li:last-child").each(function(){
alert( $(this).text() );
});
*/
window.document.write("迭代每个ul中第2个li元素中的内容,索引从1开始"+ "<br>");
/*
$("ul li:nth-child(2)").each(function(){
alert( $(this).text() );
});*/
window.document.write("在ul中查找是唯一子元素的li元素的内容"+ "<br>");
//
$("ul li:only-child").each(function() {
alert($(this).text());
});
window.document.write(" <br>");
</script>
</html>
jQuery九类选择器(一)
最新推荐文章于 2023-03-21 17:02:08 发布