2014-11-24 45 views
0

我是编程新手,很想了解我的代码中发生了什么,但与任何事情一样,这需要时间。我知道这可能是明显的与某人体验,但我不知道如何做到这一点......我一直在使用达克特的JavaScript书籍作为参考和指南。这个代码是从他那里借来的。需要指向隐藏的div

我需要能够引用和显示每个面板。我试过引用index.html#tab-1等。我不知道如何另外设置一个位置,以便它可以指向。多余的方法是为每个面板创建新的页面,但我希望内容是动态的,并且文件结构是干净的。请帮忙。

Here is a codepen

HTML结构:

<ul class="tab-list"> 
     <li class="active"><a class="tab-control" href="#tab-1">Mission &amp; Vision</a></li> 
     <li><a class="tab-control" href="#tab-2">Facilities &amp; Services</a></li> 
     <li><a class="tab-control" href="#tab-3">Policies</a></li> 
     <li><a class="tab-control" href="#tab-4">History</a></li> 
    </ul> 

    <div class="tab-panel active" id ="tab-1"> 
    <p> Content etc. </p> 

    <div class="tab-panel active" id ="tab-2"> 
    <p> Content etc. </p> 

    <div class="tab-panel active" id ="tab-3"> 
    <p> Content etc. </p> 

的Javascript:

$('.tab-list').each(function(){     // Find lists of tabs 
var $this = $(this),       // Store this list 
    $tab = $this.find('li.active'),    // Get the active list item 
    $link = $tab.find('a'),      // Get link from active tab 
    $panel = $($link.attr('href'));    // Get active panel 

$this.on('click', '.tab-control', function(e) { // When click on a tab 
e.preventDefault();       // Prevent link behavior 
var $link = $(this),       // Store the current link 
    id = this.hash;       // Get href of clicked tab 

if (id && !$link.is('.active')) {    // If not currently active 
    $panel.removeClass('active');    // Make panel inactive 
    $tab.removeClass('active');     // Make tab inactive 

    $panel = $(id).addClass('active');   // Make new panel active 
    $tab = $link.parent().addClass('active'); // Make new tab active 
    } 
}); 
}); 
+0

它可能只是我,但这听起来像一个简单的jQuery问题,似乎很多消化。你能否向我们展示不符合你期望的代码行?具体来说,你想达到什么目的?你的意思是'设置一个位置,以便它可以指向'? – 2014-11-24 15:42:34

+0

它有点听起来像你正在尝试创建模板页面?如果你想制作可以动态创建的“幻灯片”以及内容,那么学习PHP会很好。 – vulpcod3z 2014-11-24 16:15:36

+0

Php绝对是下一步。以下是我需要发生的事情: 1.用户具有指向面板内容的链接。 index.html#tab-1 2.当用户转到链接tab-1时打开 由于阻止默认链接行为,我无法通过它当前编码的方式来实现此目的。但是,我不确定如何在不执行此操作的情况下让选项卡正常工作。 – Poppe 2014-11-24 17:22:52

回答

0

更新codepen(http://codepen.io/anon/pen/JodKEd#load-tab-2)与内部和exetnal链接工作。 (固定.tab-control点击功能)

$('.tab-list li').on('click', '.tab-control', function(e){ 
    e.preventDefault(); 
    id = this.hash; 
    activateTab(id); 
}); 

$("a.direct").on('click',function(e){ 
    e.preventDefault(); 
    id = this.hash; 
    activateTab(id); 
}); 

function activateTab(id){ 
    var li = $("li a[href="+id+"]").parent(); 
    if (id && !li.is('.active')) { 
     $("li.active").removeClass('active'); 
     $('.tab-panel').removeClass('active'); 
     $(id).addClass('active');  
     $("li a[href="+id+"]").parent().addClass("active"); 
    } 
} 

if(document.location.hash){ 
    //if(write_here_some_check_for_valid_hash: #load-tab-x){ 
     var hash = document.location.hash.split("-"); 
     var id = "#"+hash[1]+"-"+hash[2]; 
     activateTab(id); 
    //} 
} 

在外部连结用#load-tab-2散列加载tab-2。散列中的load前缀是为了防止页面自动滚动。

+0

谢谢!我知道这是一个简单的修复...你能向我解释为什么这有效吗? – Poppe 2014-11-24 20:20:33

+0

我把它整合到了现场,http://www.iu.edu/~celt/WebsiteRedesign/test/about/index.html,但它仍然不起作用。 – Poppe 2014-11-24 20:26:01

+0

链接在哪里? – MIvanIsten 2014-11-24 22:03:12