js實現簡單的可切換選項卡效果

來源:互聯網
上載者:User

js實現簡單的可切換選項卡效果

 ,最簡單的純粹的選項卡

第一步,當然是先寫html代碼和css樣式

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

<!DOCTYPE html>

<html>

<head>

<meta charset="gb2312" />

<title>無標題文檔</title>

<style>

body,ul,li{margin:0; padding:0; font:12px/1.5 arial;}

ul,li{list-style:none;}

.wrap{width:500px; margin:20px auto;}

.hide{display:none;}

#tab_t{height:25px;border-bottom:1px solid #ccc;}

#tab_t li{float:left; width:80px; height:24px;

line-height:24px; margin:0 4px; text-align:center;

border:1px solid #ccc; border-bottom:none;

background:#f5f5f5; cursor:pointer}

#tab_t .act{ position:relative; height:25px;

margin-bottom:-1px; background:#fff;}

#tab_c{border:1px solid #ccc;

border-top:none; padding:20px;}

</style>

</head>

<body>

<div class="wrap">

<ul id="tab_t">

<li class="act">選擇1</li>

<li>選擇2</li>

<li>選擇3</li>

<li>選擇4</li>

</ul>

<div id="tab_c">

<div>內容1</div>

<div class="hide">內容2</div>

<div class="hide">內容3</div>

<div class="hide">內容4</div>

</div>

</div>

</body>

</html>

第二步,實現簡單的轉場效果

要點1:

1

abc.document.getElementsByTagName("li")

取得abc下面的所有標籤為li的元素,返回的是一個元素集合,有數組的一些屬性。

要點2:迴圈,先迴圈給li加上onclick事件,再onlink事件點擊的時候,再迴圈讓所有選項卡的act樣式去掉,所有的內容隱藏。然後讓所點擊的選項及對應內容顯示。
要點3:

1

tab_t_li[i].index = i;

在迴圈時,給選項卡加一個額外的屬性並賦值,以做選項卡和內容的對應。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

<!DOCTYPE html>

<html>

<head>

<meta charset="gb2312" />

<title>無標題文檔</title>

<style>

body,ul,li{margin:0; padding:0; font:12px/1.5 arial;}

ul,li{list-style:none;}

.wrap{width:500px; margin:20px auto;}

.hide{display:none;}

#tab_t{

height:25px;

border-bottom:1px solid #ccc;

}

#tab_t li{

float:left;

width:80px;

height:24px;

line-height:24px;

margin:0 4px;

text-align:center;

border:1px solid #ccc;

border-bottom:none;

background:#f5f5f5;

cursor:pointer

}

#tab_t .act{

position:relative;

height:25px;

margin-bottom:-1px;

background:#fff;

}

#tab_c{

border:1px solid #ccc;

border-top:none;

padding:20px;

}

</style>

<script>

window.onload = function(){

var tab_t = document.getElementById("tab_t");

var tab_t_li = tab_t.getElementsByTagName("li");

var tab_c = document.getElementById("tab_c");

var tab_c_li = tab_c.getElementsByTagName("div");

var len = tab_t_li.length;

var i=0;

for(i=0; i<len; i++){

tab_t_li[i].index = i;

tab_t_li[i].onclick = function(){

for(i=0; i<len; i++){

tab_t_li[i].className = '';

tab_c_li[i].className = 'hide';

}

tab_t_li[this.index].className = 'act';

tab_c_li[this.index].className = '';

}

}

}

</script>

</head>

<body>

<div class="wrap">

<ul id="tab_t">

<li class="act">選擇1</li>

<li>選擇2</li>

<li>選擇3</li>

<li>選擇4</li>

</ul>

<div id="tab_c">

<div>內容1</div>

<div class="hide">內容2</div>

<div class="hide">內容3</div>

<div class="hide">內容4</div>

</div>

</div>

</body>

</html>

第三步,寫成函數。上面的寫法只能一個頁面用一個選項卡,如果再加一個的話,就需要複製一份,再改很多變數名。

要點:tab_t_li[i][evt] 因為傳值的時候是字串,如果直接寫的話就是tab_t_li[i]."onclick"這樣話是執行不了的,tab_t_li["onclick"]這樣執行沒問題。

好了,現在一個頁面上就可以有多個切換了,只需要調用函數的時候,寫上相應的id名和標籤名,事件名稱就可以了

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

<!DOCTYPE html>

<html>

<head>

<meta charset="gb2312" />

<title>無標題文檔</title>

<style>

body,ul,li{margin:0; padding:0; font:12px/1.5 arial;}

ul,li{list-style:none;}

.wrap{width:500px; margin:20px auto;}

.hide{display:none;}

#tab_t{

height:25px;

border-bottom:1px solid #ccc;

}

#tab_t li{

float:left;

width:80px;

height:24px;

line-height:24px;

margin:0 4px;

text-align:center;

border:1px solid #ccc;

border-bottom:none;

background:#f5f5f5;

cursor:pointer

}

#tab_t .act{

position:relative;

height:25px;

margin-bottom:-1px;

background:#fff;

}

#tab_c{

border:1px solid #ccc;

border-top:none;

padding:20px;

}

</style>

<script>

window.onload = function(){

tab("tab_t","li","tab_c","div","onmouseover")

function tab(tab_t,tab_t_tag,tab_c,tag_c_tag,evt){

var tab_t = document.getElementById(tab_t);

var tab_t_li = tab_t.getElementsByTagName(tab_t_tag);

var tab_c = document.getElementById(tab_c);

var tab_c_li = tab_c.getElementsByTagName(tag_c_tag);

var len = tab_t_li.length;

var i=0;

for(i=0; i<len; i++){

tab_t_li[i].index = i;

tab_t_li[i][evt] = function(){

for(i=0; i<len; i++){

tab_t_li[i].className = '';

tab_c_li[i].className = 'hide';

}

tab_t_li[this.index].className = 'act';

tab_c_li[this.index].className = '';

}

}

}

}

</script>

</head>

<body>

<div class="wrap">

<ul id="tab_t">

<li class="act">選擇1</li>

<li>選擇2</li>

<li>選擇3</li>

<li>選擇4</li>

</ul>

<div id="tab_c">

<div>內容1</div>

<div class="hide">內容2</div>

<div class="hide">內容3</div>

<div class="hide">內容4</div>

</div>

</div>

</body>

</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.