jquery小练习,做个选项卡

点击选项卡标签,可以显示不同的内容,截图如下:

代码如下:

 1 <!DOCTYPE html>
 2 <html lang="zh-hans">
 3
 4 <head>
 5     <meta charset="utf-8">
 6     <title>My Webpage</title>
 7     <style type="text/css">
 8         *{
 9             margin: 0;
10             padding: 0;
11         }
12         .table_card{
13             width:500px;
14             margin: auto;
15             background-color: #eee;
16         }
17         .nav_tab{
18             width: 100%;
19             float: left;
20         }
21         .nav_tab ul li{
22             list-style: none;
23             float: left;
24             background-color: #ccc;
25             border-right: 1px solid #333;
26             line-height: 1.5em;
27             padding: 0 0.5em;
28         }
29         .nav_tab ul{
30             float: left;
31         }
32         .content{
33             border: 1px solid #333;
34             float: left;
35             width: 99%;
36         }
37         .clear{
38             clear: both;
39         }
40         .hide{
41             display: none;
42         }
43         ul li.current{
44             background-color: #ff9900;
45         }
46     </style>
47     <script type="text/javascript" src="jquery.js"></script>
48     <script type="text/javascript">
49         $(function(){
50             //init the content
51             $("div.current").siblings().addClass("hide");
52
53             $("li").click(function(){
54                 var $tag_name = $(this).attr("class").toLowerCase();
55                     $content_selecter = "div." + $tag_name;
56                 // highlight the table tag
57                 $(this).addClass("current").siblings().removeClass("current");
58                 //show the current content, and hide other content divs
59                 $($content_selecter).removeClass("hide").siblings().addClass("hide");
60             });
61
62
63         });
64
65     </script>
66 </head>
67 <body>
68     <div class="table_card">
69
70         <div class="nav_tab">
71             <ul>
72                 <li class="current news">News</li>
73                 <li class="sports">Sports</li>
74                 <li  class="movies" style="border-right:none;">Movies</li>
75             </ul>
76         </div>
77
78         <div class="content">
79             <div class="news current">
80                 this is news
81             </div>
82
83             <div class="sports">
84                 this is sports
85             </div>
86
87             <div class="movies">
88                 this is movies
89             </div>
90         </div>
91         <div class="clear"></div>
92     </div>
93 </body>
94
95 </html>
时间: 2024-08-04 17:02:43

jquery小练习,做个选项卡的相关文章

Jquery:小知识;

Jquery:小知识: jQuery学习笔记(二):this相关问题及选择器 上一节的遗留问题,关于this的相关问题,先来解决一下. this的相关问题 this指代的是什么 这个应该是比较好理解的,this就是指代当前操作的DOM对象. 在jQuery中,this可以用于单个对象,也可以用于多个对象. $('btn').click(function(){ alert(this.innerHTML); // 单个对象,this指代当前id为btn的DOM对象 }); $('div').each

程序员都会的 35 个 jQuery 小技巧

收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发. 1. 禁止右键点击 1 $(document).ready(function(){ 2 $(document).bind("contextmenu",function(e){ 3 return false; 4 }); 5 }); 2. 隐藏搜索文本框文字 1 Hide when clicked in the search field, the value.(example can be found below in t

必须学会使用的35个Jquery小技巧

query 与JS相比较,方便了许多,语法上也简便了不少,下面是Jquery常用的技巧,下面Jquery使用的方法在各个网站中都会使用到. 收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发. 1. 禁止右键点击 1 2 3 4 5 $(document).ready(function(){     $(document).bind("contextmenu",function(e){         return false;     }); }); 2. 隐藏搜索文本框

10个 jQuery 小技巧

10个 jQuery 小技巧 -----整理by: xiaoshuai 1. 返回顶部按钮 可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. // Back to top                                        $('a.ktop').click(function () {                          $(document.body).animate({scrollTop: 0}, 800)

每个程序员都会的 35 个 jQuery 小技巧

1 收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发. 2 1. 禁止右键点击 3 $(document).ready(function(){ 4 $(document).bind("contextmenu",function(e){ 5 return false; 6 }); 7 }); 8 2. 隐藏搜索文本框文字 9 Hide when clicked in the search field, the value.(example can be found belo

人人都会的35个Jquery小技巧

收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发. 1. 禁止右键点击 $(document).ready(function(){ $(document).bind("contextmenu",function(e){ return false; }); }); 2. 隐藏搜索文本框文字 Hide when clicked in the search field, the value.(example can be found below in the comment f

人人必知的10个 jQuery 小技巧

原文地址:http://info.9iphp.com/10-jquery-tips-everyone-should-know/ 人人必知的10个 jQuery 小技巧 收集的10个 jQuery 小技巧/代码片段,可以帮你快速开发. 1. 返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. // Back to top $('a.top').click(function () { $(document.body).animate({sc

关于jQuery小认知(后期会补充)

$这个符号在Jquery中并不陌生,什么都会用到它,$表示着jquery.一般用于自我复习( 内容部分摘于他人) $()可以是$(expresion),即css选择器. eg:$("div")   表示页面中的所有div元素.   1 $(document).ready(function(){ //这个函数的执行,是在页面加载完了之后执行,一些方法放这个函数执行,是因为如果页面没有加载完,就执行操作会出现错误 2 3 $("button").click(functi

jQuery小盒子菜单效果

jQuery小盒子菜单效果 程序吧推荐下载,小盒子方式菜单,效果酷毙了... jQuery小盒子菜单效果,布布扣,bubuko.com

实用的 jquery 小插件

1 /** 2 * ---------------------------------------------------- 3 * 实用的 jquery 插件方法 4 * @QQ 617937424 5 * @寄语 不要重复写同样的代码,学会封装成方法,慢慢积累,提高效率. 6 * ---------------------------------------------------- 7 */ 8 9 10 /** 11 * 全选/反选 12 * 13 * html结构: 14 <div i