怎么把这两段合并为一个方法呢?他们结构是一样的_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:35:15
原创
1261人浏览过

结构:
.s-content >.tab
.s-content >.inner
想要的结果是在不同大层里点击tab切换对应顺序的inner

  	 //网址tab  	 var wtag=$("#s_nall_tabs a");  	 wtag.each(function(){  	 		$(this).click(function(i){  	 			wtag.removeclass("tab-on");  	 				var index=$("#s_nall_tabs a").index(this);  	 				$(this).addclass("tab-on");  	 				$("#s_nall_tabs_con .inner").hide();  	 				$("#s_nall_tabs_con .inner").eq(index).show();  	 		})  	 });  	   	 //网址tab  	 var stag=$(".s-gbuy-hd a");  	 stag.each(function(){  	 		$(this).click(function(i){  	 			stag.removeclass("tab-on");  	 				var index=$(".s-gbuy-hd a").index(this);  	 				$(this).addclass("tab-on");  	 				$(".s-gbuy .inner").hide();  	 				console.log(index);  	 				$(".s-gbuy .inner").eq(index).show();  	 		})  	 });
登录后复制


回复讨论(解决方案)

 var wTag=$("#s_nall_tabs a,.s-gbuy-hd a");
       wTag.each(function(){
               $(this).click(function(i){
                   wTag.removeClass("tab-on");
                       var index=$("#s_nall_tabs a").index(this);
                       $(this).addClass("tab-on");
                       $("#s_nall_tabs_con .inner").hide();
                       $("#s_nall_tabs_con .inner").eq(index).show();
               })
       });

 var wTag=$("#s_nall_tabs a,.s-gbuy-hd a");
       wTag.each(function(){
               $(this).click(function(i){
                   wTag.removeClass("tab-on");
                       var index=$("#s_nall_tabs a").index(this);
                       $(this).addClass("tab-on");
                       $("#s_nall_tabs_con .inner").hide();
                       $("#s_nall_tabs_con .inner").eq(index).show();
               })
       });




哇,原来可以这样。。。。我感动哭了要。。。。谢谢  ~




<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Tab</title></head><body>    <style>        .tab-on { background-color: green; }    </style>    <div class="s-content">        <div class="tab">            <a class="tab-on" href="#" id="tab-1">Tab-1</a>            <a href="#" id="tab-2">Tab-2</a>            <a href="#" id="tab-3">Tab-3</a>        </div>        <div class="inner" data-for="tab-1">            <p>tab-1 content!</p>        </div>        <div class="inner" data-for="tab-2" style="display: none;">            <p>tab-2 content!</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/1672">
                            <img src="https://img.php.cn/upload/ai_manual/000/000/000/175680338913523.png" alt="BibiGPT-哔哔终结者">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/1672">BibiGPT-哔哔终结者</a>
                            <p>B站视频总结器-一键总结 音视频内容</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="BibiGPT-哔哔终结者">
                                <span>28</span>
                            </div>
                        </div>
                        <a href="/ai/1672" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="BibiGPT-哔哔终结者">
                        </a>
                    </div>
                        </div>        <div class="inner" data-for="tab-3" style="display: none;">            <p>tab-3 content!</p>        </div>    </div>    <script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js"></script>    <script>        $( ".s-content .tab a" ).click( function () {            var tabIndex = $( this ).index();            $( this ).addClass( "tab-on" ); // 给点击的 tab 加上 “.tab-on”            $( this ).siblings().removeClass( "tab-on" ); // 其他的 tab 去掉 “.tab-on”            $( ".s-content .inner" ).hide().eq( tabIndex ).show();        } );    </script></body></html>
登录后复制

var wTag=$(".s-content .tab");       wTag.each(function(){               $(this).click(function(i){                       var p=$(this).parents(".s-content");                       var tabs=p.find(".tab");                       var index=tabs.index(this);                       tabs.removeClass("tab-on");                       $(this).addClass("tab-on");                       p.find(".inner").hide();                       p.find(".inner").eq(index).show();               })       });
登录后复制


再修理好点~



<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Tab</title></head><body>    <style>        .tab-on { background-color: green; }    </style>    <div class="s-content">        <div class="tab">            <a class="tab-on" href="#" id="tab-1">Tab-1</a>            <a href="#" id="tab-2">Tab-2</a>            <a href="#" id="tab-3">Tab-3</a>        </div>        <div class="inner" data-for="tab-1">            <p>tab-1 content!</p>        </div>        <div class="inner" data-for="tab-2" style="display: none;">            <p>tab-2 content!</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p>        </div>        <div class="inner" data-for="tab-3" style="display: none;">            <p>tab-3 content!</p>        </div>    </div>    <script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js"></script>    <script>        $( ".s-content .tab a" ).click( function () {            var tabIndex = $( this ).index();            $( this ).addClass( "tab-on" ); // 给点击的 tab 加上 “.tab-on”            $( this ).siblings().removeClass( "tab-on" ); // 其他的 tab 去掉 “.tab-on”            $( ".s-content .inner" ).hide().eq( tabIndex ).show();        } );    </script></body></html>
登录后复制


这个当页面有多个s-content 就不行了的
HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号