< body> < div class = " tab" > < div class = " tab_list" > < ul> < li class = " current" > 商品介绍</ li> < li> 规格与包装</ li> < li> 售后包装</ li> < li> 商品评价(50000)</ li> < li> 手机社区</ li> </ ul> </ div> </ div> < div class = " tab_con" > < div class = " item" style =" display : block; " > 商品介绍模块内容</ div> < div class = " item" > 规格与包装模块内容</ div> < div class = " item" > 售后保障模块内容</ div> < div class = " item" > 商品评价(50000)模块内容</ div> < div class = " item" > 手机社区模块内容</ div> </ div> </ body>
* { margin : 0; padding : 0; } .tab_list ul { margin-top : 50px; margin-left : 200px; background-color : #e8e8e8; width : 800px; height : 39px; border : 1px solid red; } .tab_list li { list-style : none; float : left; height : 39px; line-height : 39px; padding : 0 20px; text-align : center; cursor : pointer; } .tab_list .current { background-color : red; color : white; } .tab_con { margin-left : 200px; } .item { color : #686868; display : none; }
< script type= "text/javascript" > var lis = document. querySelector ( '.tab_list' ) . querySelectorAll ( 'li' ) ; var items = document. querySelectorAll ( '.item' ) ; for ( var i = 0 ; i < lis. length; i++ ) { lis[ i] . setAttribute ( 'index' , i) ; lis[ i] . onclick = function ( ) { for ( var i = 0 ; i < lis. length; i++ ) { lis[ i] . className = '' ; } this . className = 'current' ; var index = this . getAttribute ( 'index' ) ; console. log ( index) ; for ( var i = 0 ; i < items. length; i++ ) { items[ i] . style. display = 'none' ; } items[ index] . style. display = 'block' ; } } < / script>
实现效果: