简体   繁体   English

根据单击的 TAB 更改 DIV

[英]Change DIV based on clicked TAB

I am doing a leaderboard for a website we are working on.我正在为我们正在开发的网站制作排行榜。

Essentially, we have a div with this months winner for location A本质上,我们有一个 div 与本月位置 A 的获胜者

Below we have ajax tabs, where user can click tabs which relate to locations, like:下面我们有 ajax 选项卡,用户可以在其中单击与位置相关的选项卡,例如:

Location A Location B etc etc位置 A 位置 B 等

So by default, when page loads, tab A is open.所以默认情况下,当页面加载时,选项卡 A 是打开的。 And the div above we need to give a matching ID, because...而上面的div我们需要给一个匹配的ID,因为...

I want as the user clicks tab B for the div above to change, with different DIV ID.我希望用户单击选项卡 B 以更改上面的 div,并使用不同的 DIV ID。 So basically we can change content in the div based on the tab the user clicks.所以基本上我们可以根据用户点击的标签来改变 div 中的内容。

So the content div is like:所以内容 div 就像:

<div id="???"> content goes here </div>

The tabs are like:标签如下:

<ul class="tabs"> 
                    <li><a href="#tab1"><span class="stateTab">NSW</span></a></li> 
                    <li><a href="#tab2"><span class="stateTab">QLD</span></a></li> 
                    <li><a href="#tab3"><span class="stateTab">VIC</span></a></li> 
                    <li><a href="#tab4"><span class="stateTab">SA</span></a></li> 
                    <li><a href="#tab5"><span class="stateTab">WA</span></a></li> 
                    <li><a href="#tab6"><span class="stateTab">ACT</span></a></li> 
                    <li><a href="#tab7"><span class="stateTab">NT</span></a></li> 
                    <li><a href="#tab8"><span class="stateTab">TAS</span></a></li>
                    <li><a href="/leaderboard/australia/"><span class="stateTab">AUSTRALIA</span></a></li>
                </ul> 

So if user clicks #tab2 then a different DIV loads into the div id="???".因此,如果用户单击#tab2,则会将不同的 DIV 加载到 div id="???" 中。

I think its fairly simple, just cannot figure it out.我认为它相当简单,只是无法弄清楚。 I realise I possibly have to set all the divs up, like so:我意识到我可能必须设置所有的 div,如下所示:

<div id="tab1"> content goes here </div>
<div id="tab2"> content goes here </div>
<div id="tab2"> content goes here </div>

And set visibility hidden to the divs.. any help appreciated.并设置隐藏到 div 的可见性.. 任何帮助表示赞赏。

*** *** ADDED INFO添加信息** * **** ** * ****

The tabs, onclick ( presently ) display content from dataTables.选项卡 onclick(目前)显示数据表中的内容。 So obviously when we click on tab1, the content below the tabs, shows the content fetched from our dataTables, in a div with id1所以很明显,当我们点击 tab1 时,选项卡下方的内容显示了从我们的 dataTables 中获取的内容,在 id1 的 div 中

The issue now is, with wanting to change the content ABOVE the tabs aswell as the content BELOW the tabs... the 2 id's are conflicting, and one shows and one hides...现在的问题是,想要更改选项卡上方的内容以及选项卡下方的内容...... 2 个 id 是冲突的,一个显示一个隐藏......

The TABS also change content below them, presumably we need to chain the id actions somehow, to get two sets of content to change in harmony TABS 也改变了它们下面的内容,大概我们需要以某种方式链接 id 动作,以使两组内容和谐地改变

Set it up the way you planned in HTML adding style="display: none" to each div except the one you want to show by default.按照您在 HTML 中计划的方式设置它,将 style="display: none" 添加到每个 div,除了您要默认显示的那个。 Then add to you javascript (at the bottom, or in $(function(){ //Document ready });然后给你添加 javascript (在底部,或者在$(function(){ //Document ready });

$('.tabs a').click(function(){
        $('div[id^=tab]').hide();
        $(this.href).show();
        return false;
    }
);

As for your Update, you can change your divs to have a class instead of an id.至于您的更新,您可以将您的 div 更改为具有 class 而不是 id。 Like Content Above 1 Content Above 2喜欢 1 以上的内容 2 以上的内容

Tabs标签

 <div class="tab1 tabContent">Content Below 1</div>
 <div class="tab2 tabContent">Content Below 2</div>

Then you can change the javascript:然后您可以更改 javascript:

$('.tabs a').click(function(){
        $('div.tabContent').hide();
        $('div.'+this.href).show();
        return false;
    }
);

You'll also need to remove the hashes from your anchors, so the href becomes "tab1" instead of "#tab1"您还需要从锚点中删除哈希,因此 href 变为“tab1”而不是“#tab1”

You could use jQuery to do this: http://jsfiddle.net/YQdQm/您可以使用 jQuery 来执行此操作: http://jsfiddle.net/YQdQm/

Not sure if this meets your requirements exactly (also, haven't yet tested on IE).不确定这是否完全符合您的要求(另外,尚未在 IE 上测试)。

var tabs = $('div[id^=tab]');
tabs.hide();
$('#tab1').show();
$('.tabs a').click(function () {
    var tab_id = $(this).attr('href'); 
    tabs.hide();
    $(tab_id).show(); 
});

I would suggest use existing tab control from jquery UI我建议使用 jquery UI 中的现有选项卡控件

however if not you will need markup like that但是,如果不是,您将需要这样的标记

<div class='tabs'>
<ul>
<li data-id='tab1'>tab 1</li>
....
</ul>
<div id='tab1'></div>
<div id='tab2' class='expanded'></div>
...
</div>

and code和代码

$('.tabs ul li').bind('click',function() {
    var id = $(this).data('id');
    $('.tabs div').removeClass('expanded');
    $('#'+id).addClass('expanded');    
});

I know this is a bit brute force, but I like to do it this way:我知道这有点蛮力,但我喜欢这样做:

JS: JS:

    function hidetabs(){
     $("#tab1").hide();
     $("#tab2").hide();
     //And so on
    }

   function shobwtab(id){ 
    $("#"+id).show();
      hidetabs();
   }

HTML: HTML:

   <li><a href="#tab1" onClick="showtab('tab1')"><span class="stateTab">NSW</span></a></li>

Of course you could also add click listeners in your docready function to run the functions instead of using onClick.当然,您也可以在您的文档 function 中添加点击侦听器来运行这些函数,而不是使用 onClick。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM