2015-04-22 41 views
0

选择器如何设置我的选择器是特定于每个h1和主多重复结构。我需要能够点击一个h1并打开包含在同一部分中的HTML。根据javascript的下面,请回答什么var触发器和var内容应该是完成要求。jQuery的multipe slideToogle

<section id="side_nav"> 
     <!-- Main --> 
     <section> 
      <header> 
       <h1>Main Header</h1> 
       <a href="#">Call to action</a> 
      </header> 
      <main> 
       <div> 
        <p><span>content</span><span>content</span></p> 
        <p><span>content</span><span class="positive">content</span></p> 
       </div> 
      </main> 
     </section> 
     <!-- First sub --> 
     <section> 
      <header> 
       <h1>First sub header</h1> 
       <p>Content</p> 
      </header> 
      <main> 
       <div> 
        <p><span>content</span><span>content</span></p> 
        <p><span>content</span><span>content</span></p> 
       </div>   
      </main> 
     </section> 
     <!-- Second sub --> 
     <section> 
      <header> 
       <h1>Second sub header</h1> 
       <p>content</p> 
      </header> 
      <main> 
       <div> 
        <p><span>content</span><span>content</span></p> 
        <p><span>content</span><span>content</span></p> 
       </div>   
      </main> 
     </section> 
     <!-- Third sub --> 
     <section> 
      <header> 
       <h1>Third sub header</h1> 
       <p>content</p> 
      </header> 
      <main> 
       <div> 
        <p><span>content</span><span>content</span></p> 
        <p><span>content</span><span>content</span></p> 
       </div>   
      </main> 
     </section> 
    </section> 

我现在的JS是这样的:

$(document).ready(function(){ 
     var trigger = $("h1"); 
     var content = $("main"); 

     trigger.click(function(){ 
      content.slideToggle("slow"); 
     }); 
    }); 

这里为的jsfiddle与我当前的代码:

http://jsfiddle.net/vinicius5581/fLyy931s/

+0

你需要停止改变人们的答案,并说他们不工作......没有人在点击处理程序之外使用“内容”变量的原因是因为你失去了这一点的范围,它不会有与你正在寻找的H1相关的价值。 – Trey

回答

2

的问题是,你指的是所有您的文档中的<content>元素,而不是指您的旁边的元素0。

试试这个:

$(document).ready(function(){ 
    var trigger = $("h1"); 

    trigger.click(function(){ 
     $(this).parent().next('main').slideToggle("slow"); 
    }); 
}); 

Fiddle

+0

这没有奏效。 http://jsfiddle.net/vinicius5581/fLyy931s/5/ –

+0

@ViniciusSantana你没有尝试我提供的代码。 – haim770

2

你可能需要的东西,如:

$(document).ready(function(){ 
    $("h1").click(function(){ 
     $(this).closest('section').find('main').slideToggle("slow"); 
    }); 
}); 

.closest方法发现,选择相匹配的最接近的父母,抓住你可以找到主要的孩子和你的功能将永远与结构有关。

要扩大为什么你的“这行不通”的评论是错误的:

$(document).ready(function(){ 
    //when you do this... 
    var trigger=$('h1'); 
    //You are saying "grab every h1 on the page" 

    //when you do this... 
    var content=$('main'); 
    //you are grabbing EVERY "main" on the page 

    //so when you do this 
    trigger.click(function(){ 
     //you apply a click handler to every h1 on the page 

     //now once in here when you reference "content" you are actually 
     //referencing EVERY "main" on the page since you already assigned 
     //that collection to a variable outside of the click handler 

     //the reason we all used a variation of this: 

     $(this).closest('section').find('main').slideToggle("slow"); 

     // is because $(this) now points to the specific h1 that was clicked 
     // on, so by navigating the DOM using the available jQuery methods 
     // we can actially get the "main" element that relates to the h1 
    }); 
}); 

基本上你背着一个全球范围内为事件处理程序,并假设它有一些动态的方式来确定上下文。这可能使一些更有意义是这样的:

$(document).ready(function(){ 
    var triggers=$('h1'); 
    //let's call it "triggers" since this will actually return every h1 on the page. 

    triggers.click(function(){ 
     //Grab the parent of the thing that was clicked on so we can find the 
     //right content to toggle 

     var context=$(this).closest('section'); 

     //When you send a second argument in to jQuery like this you tell 
     // it where to look for the elements you've selected 
     var content=$('main',context); 

     //NOW you can use your variable knowing that you have the right 
     //piece of content 
     content.slideToggle("slow"); 
    }); 
}); 
+0

这没有奏效。 http://jsfiddle.net/vinicius5581/fLyy931s/7/ –

+0

它绝对没有工作http://jsfiddle.net/uzvbqa0b/ – Trey

+0

你改变了我的代码在你的小提琴......这工作完全按照设计 – Trey

4

您需要引用this在您的触发功能,然后遍历DOM的内容你是显示:

trigger.click(function(){ 
    $(this).parent().siblings('main').slideToggle("slow"); 
}); 

而且你可能想一些CSS最初隐藏<main>内容:

main { 
    display:none; 
} 

更新小提琴:http://jsfiddle.net/fLyy931s/2/

+0

这没有奏效。 http://jsfiddle.net/vinicius5581/fLyy931s/6/ –

+0

它实际上工作,但如果我把选择器放在一个变量是休息。 :p –

+0

通常,当您为jQuery选择器创建'var'时,例如'$(“main”)',您使用美元符号来提醒您它是jQuery对象,而不是字符串,如下所示:'$ content '。因为在'兄弟()'函数中我传递了一个STRING。 – inorganik