jQuery is()函數用法3例

來源:互聯網
上載者:User

標籤:style   http   io   color   ar   os   使用   java   sp   

這篇文章主要介紹了jQuery is()函數用法3例,jQuery的is()函數其實是非常有用的,能不能用好直接決定你的代碼是否高效,需要的朋友可以參考下。

jQuery提供了is()方法可以很方便的判斷元素是否可見,是否隱藏,是否選中。

一、判斷元素是否隱藏

如下html中的div元素是隱藏的:
代碼如下:
<!doctype html><html>    <head>        <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.2.min.js"></script>    </head>    <body>        <div id="test" style="display:none">你看不到我</div>        <span ></span>         <script type="text/javascript">            $(‘span‘).html(‘test div is visible? ‘ + $(‘#test‘).is(‘:visible‘));        </script>    </body></html>




二、判斷checkbox是否選中

jquery中可以通過xx.is(‘:checked‘)判斷checkbox,radiobutton是否是選中狀態,如下測試html
代碼如下:

<!doctype html><html>    <head>        <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.2.min.js"></script>    </head>    <body>        <p>            <input type="checkbox" name="chkChecked" checked="checked"/> chkChecked            <input type="checkbox" name="chkNoChecked" /> chkNoChecked        </p>        <span ></span>        <script type="text/javascript">            $(‘span‘).html(‘chkChecked checked? ‘ + $(‘input[name=chkChecked]‘).is(‘:checked‘) + ‘<br/> ‘ +‘chkNoChecked checked? ‘ + $(‘input[name=chkNoChecked]‘).is(‘:checked‘) );        </script>    </body></html> 





三、判斷是否使用了某個樣式

代碼如下:

<html lang="en">     <head>        <meta charset="utf-8">        <title>is函數介紹</title>        <style>  //設定一些基本樣式              div { width:60px; height:60px; margin:5px; float:left;border:4px outset; background:green; text-align:center;font-weight:bolder; cursor:pointer; }              .blue { background:blue; }  //樣式1              .red { background:red; }//樣式2              span { color:white; font-size:16px; }              p { color:red; font-weight:bolder; background:yellow;margin:3px; clear:left; display:none; }        </style>        <script src="http://code.jquery.com/jquery-1.9.1.js"></script> //注意 這裡使用的是jQuery官方的腳步庫    </head>    <body>        <div></div>  //注意這裡出現了第一個div        <div class="blue"></div>//注意這裡出現了第2個div        <div></div>//注意這裡出現了第3個div        <div class="red"></div>//注意這裡出現了第4個div        <div><br/><span>Peter</span></div>//注意這裡出現了第5個div        <div class="blue"></div>//注意這裡出現了第6個div        <p> </p>        <script>             $("div").one(‘click‘, function () {    //$("div").one  代表對div元素附加一個事件,                //還能附加多個事件 譬如click或者mouseout時同時執行一些事情                if ($(this).is(":first-child")) { //is函數發揮作用了,is(":first-child") 代表                     //判斷此div是否第一個出現的div                    $("p").text("It‘s the first div."); //text和html區別在於是否支援html標記                     // 此時你在裡面寫個 alert是不會執行的                 } else if ($(this).is(".blue,.red")) {  //判斷該div是否 具有blue或者red的 class                    $("p").text("這是個藍色或者紅色的div");                 } else if ($(this).is(":contains(‘Peter‘)")) { //判斷div中是否存在Peter這個詞                    $("p").text("It‘s Peter!");                 } else {                    $("p").html("It‘s nothing <em>special</em>.");                 }                 $("p").hide().slideDown("slow"); //這是一個動畫效果。慢慢展現p的內容                 $(this).css({"border-style": "inset", cursor:"default"});            });        </script>    </body></html> 





jQuery is()函數用法3例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.