標籤:ddc tom remove 如何 border pointer addclass 部分 是你
啦啦!今天又學了一招,js寫幾十行的tab標籤頁jquery寫一行就行啦,用到了鏈式操作!以下是代碼:
<!DOCTYPE html><html lang="en"><head><style> *{ padding: 0; margin: 0; } ul{ list-style-type: none; } #ul{ height: 30px; margin-bottom: 10px; } #ul li { height: 30px; line-height: 30px; padding: 0 15px; border: 1px solid gray; float: left; margin-right: 3px; cursor: pointer; } #ul li.current { background: #abcdef; } #content div{ width: 300px; height: 200px; border: 1px solid red; border-collapse: collapse; display: none; } #content div.php{ display: block; }</style> <script src = ‘js.js‘></script> <meta charset="UTF-8"> <title>Document</title></head><body> <ul id="ul"> <li class = ‘current‘>php</li> <li>ruby</li> <li>python</li> </ul> <div id="content"> <div class = "php">php。。。。。。介紹</div> <div class = "ruby">ruby。。。。。。介紹</div> <div class = "python">python。。。。。。介紹</div> </div> <script> $(‘li‘).click(function(){ //$(this).css(‘background‘,‘#abcdef‘); //$(this).siblings().css(‘background‘,‘white‘); $(this).addClass(‘current‘).siblings().removeClass(‘current‘).parent().next().children().eq($(this).index()).show().siblings().hide(); //添加class //var m = $(this).html(); //$(‘.‘+m).css(‘display‘,‘block‘).siblings().css(‘display‘,‘none‘);//class選擇法 //$(‘#content div‘).eq($(this).index()).addClass(‘php‘).siblings().removeClass(‘php‘);//索引選擇法 //$(‘#content div‘).eq($(this).index()).show().siblings().hide(); }) </script></body></html>
注釋掉的部分還有一種方法來實現關聯li和div的變化,“一行”的方法中用的是用li的索引的方法,我自己想出來的是用class選取器的方法,先將li中的文字選擇出來,然後直接用來class選擇,前提是你的div得有class名。
還有一種方法就是用內容選取器:
$(‘#content div contains(m)‘).......這種方法用於標籤和html()內容相關聯更佳靈活,代碼裡沒有寫出來,大家可以自己試一下哦!
如何一行jquery代碼寫出tab標籤頁(鏈式操作)