jquery樣式使用實例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>3-9-1</title>
<style type="text/css">
.high{
 font-weight:bold;   /* 粗體字 */
 color : red;        /* 字體顏色設置紅色*/
}
.another{
 font-style:italic;
 color:blue;
}
</style>
 <!--   引入jQuery -->
 <script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
 <script type="text/javascript">
 //<![CDATA[
  $(function(){
      //獲取樣式
      $("input:eq(0)").click(function(){
   alert( $("p").attr("class") );
   });
      //設置樣式
   $("input:eq(1)").click(function(){
   $("p").attr("class","high");
   });
      //追加樣式
   $("input:eq(2)").click(function(){
   $("p").addClass("another");
   });   
   //刪除全部樣式
   $("input:eq(3)").click(function(){
   $("p").removeClass();
   }); 
    //刪除指定樣式
   $("input:eq(4)").click(function(){
   $("p").removeClass("high");
   });  
   //重複切換樣式
   $("input:eq(5)").click(function(){
   $("p").toggleClass("another");
   }); 
   //判斷元素是否含有某樣式
   $("input:eq(6)").click(function(){
   alert( $("p").hasClass("another") )
   alert( $("p").is(".another") )
   }); 
  });
  //]]>
  </script>
</head>
<body>
    <input type="button" value="輸出class類"/>
 <input type="button" value="設置class類"/>
 <input type="button" value="追加class類"/>
 <input type="button" value="刪除全部class類"/>
 <input type="button" value="刪除指定class類"/>
 <input type="button" value="重複切換class類"/>
 <input type="button" value="判斷元素是否含有某個class類"/>

 <p class="myClass" title="選擇你最喜歡的水果." >你最喜歡的水果是?</p>
 <ul>
   <li title='蘋果'>蘋果</li>
   <li title='橘子'>橘子</li>
   <li title='菠蘿'>菠蘿</li>
 </ul>
</body>
</html>

 

發佈了31 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章