juqery学习3之juqery对象条件筛选

代码例子:某个div块下的字体样式的控制。

 1 //script代码
 2 
 3 <script src="${sitePath}/cec_wcp/js/jquery-1.8.2.min.js" type="text/javascript"></script>
 4  <script type="text/javascript">
 5     
 6         function SetFont(size){
 7                //如果id为news_content的div块下存在p标签,则控制p标签中字体的大小。
 8                //如果该id下的div块中不存在p标签,则直接控制该div下的字体的大小
 9             if($("#news_content p").length>0){
10                   $("#news_content p").css("font-size",size+"pt"); 
11                }else{
12                   $("#news_content").css("font-size",size+"pt"); 
13                }
14         }
15     
16     </script>
17 
18 //html代码
19    <p>【字体:<a href="javascript:SetFont(14)">大</a> <a href="javascript:SetFont(12)">中</a> <a href="javascript:SetFont(10)">小</a>】</p>
20 
21 <div id="news_content"></div>
View Code

第一种:jquery条件综合多条件选择器

模式:$(选择器1,选择器2)----->返回选择器1内符合选择器2的jquery对象(选择器1内的后代元素,儿子孙子辈都有)

第二种:jquery对象find()方法

模式:$(选择器1,选择器2).find("选择器3")--->返回符合选择器1,选择器2的jquery对象中包含的符合选择器3的所有后代元素。包括孙子辈。不包括自身。

 第三种:jquery对象children()方法

模式:$(选择器1,选择器2).children("选择器3")--->返回符合选择器1+选择器2的孩子级别的选择器3的jquery对象。只包含孩子辈,无孙子辈。不包括自身。

jQuery 选择器

选择器实例选取
* $("*") 所有元素
#id $("#lastname") id="lastname" 的元素
.class $(".intro") 所有 class="intro" 的元素
element $("p") 所有 <p> 元素
.class.class $(".intro.demo") 所有 class="intro" 且 class="demo" 的元素
     
:first $("p:first") 第一个 <p> 元素
:last $("p:last") 最后一个 <p> 元素
:even $("tr:even") 所有偶数 <tr> 元素
:odd $("tr:odd") 所有奇数 <tr> 元素
     
:eq(index) $("ul li:eq(3)") 列表中的第四个元素(index 从 0 开始)
:gt(no) $("ul li:gt(3)") 列出 index 大于 3 的元素
:lt(no) $("ul li:lt(3)") 列出 index 小于 3 的元素
:not(selector) $("input:not(:empty)") 所有不为空的 input 元素
     
:header $(":header") 所有标题元素 <h1> - <h6>
:animated   所有动画元素
     
:contains(text) $(":contains('W3School')") 包含指定字符串的所有元素
:empty $(":empty") 无子(元素)节点的所有元素
:hidden $("p:hidden") 所有隐藏的 <p> 元素
:visible $("table:visible") 所有可见的表格
     
s1,s2,s3 $("th,td,.intro") 所有带有匹配选择的元素
     
[attribute] $("[href]") 所有带有 href 属性的元素
[attribute=value] $("[href='#']") 所有 href 属性的值等于 "#" 的元素
[attribute!=value] $("[href!='#']") 所有 href 属性的值不等于 "#" 的元素
[attribute$=value] $("[href$='.jpg']") 所有 href 属性的值包含以 ".jpg" 结尾的元素
     
:input $(":input") 所有 <input> 元素
:text $(":text") 所有 type="text" 的 <input> 元素
:password $(":password") 所有 type="password" 的 <input> 元素
:radio $(":radio") 所有 type="radio" 的 <input> 元素
:checkbox $(":checkbox") 所有 type="checkbox" 的 <input> 元素
:submit $(":submit") 所有 type="submit" 的 <input> 元素
:reset $(":reset") 所有 type="reset" 的 <input> 元素
:button $(":button") 所有 type="button" 的 <input> 元素
:image $(":image") 所有 type="image" 的 <input> 元素
:file $(":file") 所有 type="file" 的 <input> 元素
     
:enabled $(":enabled") 所有激活的 input 元素
:disabled $(":disabled") 所有禁用的 input 元素
:selected $(":selected") 所有被选取的 input 元素
:checked $(":checked") 所有被选中的 input 元素

撒旦法

原文地址:https://www.cnblogs.com/shangxiaofei/p/3920293.html