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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值