jquery初學者學習案例

第一、層次選擇器

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>"><title>JQuery DEMO</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
        <style type="text/css">
            div{
                width:250px;
                height:100px;
                border:1px solid blue;
                float:left;
                padding:10px;
            }
            span{
                margin:3px;
                border:1px solid red;
                padding:5px;
            }
            h1,h2{
                display:inline;
            }
        </style>
        <!--引入 jquery.js 文件-->
        <script type="text/javascript" src="js/jquery/jquery-1.3.2.js">
        </script>
        <script type="text/javascript">
                 $(function(){
                    //選取div中所有的span元素樣式
                    $("div span").css("background","yellow");
                    
                    //選取div下元素是span元素樣式
                    $("div>span").css("background","blue");
                    
                    //選取id爲sp1的下一個span元素
                    //    $("#sp1 + span").css("border","3px solid black");
                    //等價於
                    $("#sp1").next("span").css("border","3px solid black");
                    
                    //選取id爲div1的所有兄弟span元素
                    $("#div1 ~ span").css("background","#eeeeee");
                    //等價於
                    //    $("#div1").nextAll("span").css("background","#eeeeee");
                
                    //不同於 siblings 與前後位置無關
                    $("#div1").siblings("span").css("background","#113344");
                });
        </script>
    </head>
    <body>
            <span>
            SPAN2
        </span>
        <div id="div1">
            <h1>div h1</h1>            
        </div>
        <div>
            <h2 >div h2</h2>
        </div>
        <div>
            <span>div span</span>
        </div>
        <div>
            <span>
                <span  id="sp1">div.div.span1</span>                
                <span>div.div.span2</span>                
            </span>
        </div>
        <span>
            SPAN1
        </span>
    </body>
</html>
第二、內容過濾選擇器

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>"><title>JQuery DEMO</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
        <style type="text/css">
            div {
                width: 250px;
                height: 100px;
                border: 1px solid black;
                float: left;
                padding: 10px;
            }
            
            span {
                margin: 3px;
                border: 1px solid black;
                padding: 5px;
            }
            
            h1, h2 {
                display: inline;
            }
        </style>
        <!--引入 jquery.js 文件-->
        <script type="text/javascript" src="js/jquery/jquery-1.3.2.js">
        </script>
        <script type="text/javascript">
            $(function(){
                //選取所有 內容有1的 div
                $("div:contains('1')").css("border","1px solid red");
                
                //選取不包含子元素(包括文本元素)的div空元素
                $("div:empty").css("background","red");
                
                //改變含有class爲mini元素的div元素的背景色
                $("div:has(.mini)").css("background","#bbffaa");
                
                //含有 子元素的div
                $("div:parent").css("background","#eeeeee");
                
                //選取所有不可見的img元素 顯示5000毫秒
                $("img:hidden").show(5000);
                
                //選取所有可見的p元素  :visible
                $("p:visible").css("border","1px dashed blue");
            });
        </script>
    </head>
    <body>
        <span>SPAN2</span>
        <div id="div1">
            <h1>div h1</h1>
            <h2>div h2</h2>
            <h1>div h3</h1>
        </div>
        <div>
            <h2>div h2</h2>
            <span>div span</span>
        </div>
        <div>
             
        </div>
        <div class="mini">
            <span><span id="sp1">div.div.span1</span><span>div.div.span2</span></span>
        </div>
        <span>SPAN1</span>
        <p><input type="text" class="normalText" value="1232"></p>
        <p><input type="password" value="1232"></p>
        <p><input type="text" value="1232"></p>
        <p><input type="text" value="1232"></p>
        
        <p>
            <textarea>132</textarea>            
            <textarea>333</textarea>            
            <textarea>444</textarea>            
            <textarea>5555</textarea>            
        </p>
        <!--此圖片緩慢顯示-->
        <img style="display:none" src="image/gun01.jpg" width="400px" height="250px">
        </img>
    </body>
</html>

第三、屬性過濾選擇器

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>"><title>JQuery DEMO</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
        <style type="text/css">
            div{
                width:250px;
                height:100px;
                border:1px solid black;
                float:left;
                padding:10px;
            }
            span{
                margin:3px;
                border:1px solid black;
                padding:5px;
            }
            h1,h2{
                display:inline;
            }
        </style>
        <!--引入 jquery.js 文件-->
        <script type="text/javascript" src="js/jquery/jquery-1.3.2.js">
        </script>
        <script type="text/javascript">
                 $(function(){
                    //選取擁有屬性id的元素
                    $("div[id]").css("color","red");
                    
                    //選取title的值爲 normal 的div元素
                    $("div[title=normal]").css("color","blue");
                    
                    //選取title的值不爲normal2 的span元素
                    $("span[title!=normal2]").css("color","red");
                    
                    //選取屬性的值以test開頭的元素
                    $("span[title^=test]").css("font-size","20");
                    
                    //選取屬性的值以3結束的元素
                    $("span[title$=3]").css("font-size","20");
                    
                    //選取屬性的值含有 normal 的元素
                    $("span[title*=normal]").css("font-size","20");
                    
                    //選取滿足多個條件 選取擁有屬性id,並且屬性title以test結束的 div 元素
                    $("div[id][title$=test]").css("color","blue");
                });
        </script>
    </head>
    <body>
        <span title="normal">
            SPAN2
        </span>
        <div id="div1">
            <h1>div h1</h1>            
        </div>
        <div title="normal">
            <h2 >div h2</h2>
        </div>
        <div>
            <span>div span</span>
        </div>
        <div>
            <span title="test123">
                <span  id="sp1" title="normal2">div.div.span1</span>                
                <span>div.div.span2</span>                
            </span>
        </div>
        <span title="normal2">
            SPAN1
        </span>
    </body>
</html>

第四、表單對象屬性過濾選擇器

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>"><title>JQuery DEMO</title>
        <style type="text/css">
        </style>
        <!--引入 jquery.js 文件-->
        <script type="text/javascript" src="js/jquery/jquery-1.3.2.js">
        </script>
        <script type="text/javascript">
                 $(function(){
                        //改變表單內可用<input>元素的值
                        $("#form1 input:enabled").val("可用變化了!");
                        
                        //改變表單內不可用<input>元素的值
                        $("#form1 input:disabled").val("不可用變化了!");
                        
                        //獲取多選框中的個數
                        alert("多選框選中的個數爲:"+$("input:checked").length);
                        
                        //獲取下拉框選中的內容
                        alert("下拉框選中的內容爲:"+$("select :selected").text());
                        
                        //獲取表單內表單元素的個數 不一定指的是 <input>標籤
                        alert("表單內input的個數:"+$("#form1 :input").length);
                        
                        //獲取表單內單行文本框的個數
                        alert("表單內text的個數:"+$("#form1 :text").length);
                });
        </script>
    </head>
    <body>
            <form id="form1" action="#">
            <p>    可用元素:<input name="add" value="可用文本框"/>
            <p>    不可用元素:<input name="add" disabled ="true" value="不可用文本框"/>
            <p>    可用元素:<input name="che" value="可用文本框"/>
            <p>
                不可用元素:<input name="chu" disabled ="true" value="不可用文本框"/>    
            <hr>
            多選框
            <p>
                <input type="checkbox" name="email" checked="checked" value="1">choose one
            </p>
            <p>
                <input type="checkbox" name="email"   value="2">choose 2
            </p>
            <p>
                <input type="checkbox" name="email"  value="3">choose 3
            </p>
            <p>
                <input type="checkbox" name="email"   value="4">choose 4
            </p>
            <p>
                <input type="checkbox" name="email" checked="checked" value="5">choose 5
            </p>
            <p>
                <select name="test1" multiple="multiple" size="5">
                    <option>浙江</option>
                    <option selected="selected">湖南</option>
                    <option>天津</option>
                    <option selected="selected">北京</option>
                    <option>湖北</option>
                </select>                
            </p>
            <p>
                <select name="test2"  >
                    <option>浙江</option>
                    <option>湖南</option>
                    <option>天津</option>
                    <option selected="selected">北京</option>
                    <option>湖北</option>
                </select>                
            </p>
            </form>
    </body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章