从外部链接打开手风琴

时间:2013-11-26 21:49:24

标签: jquery html hyperlink accordion external

我知道此问题已在此处和其他许多地方被提出过,但所提供的解决方案都没有对我有用。我不确定是不是因为我正在实施它们是错误的,或者我的脚本只是搞砸了。我正在使用jQuery UI,我的页面上有多个手风琴。我希望第二个手风琴的第一个标题从外部链接打开。这是我的剧本:

   <script type="text/javascript">
   $(function() {
   $( "#accordion" ).accordion({
      active: false,
      collapsible: true,
      heightStyle: "content",
      navigation: true
   });
   $( "#accordion2" ).accordion({
      active: false,
      collapsible: true,
      heightStyle: "content",
      navigation: true
   });
   </script>


   <div>
   <h7 id="misc">Misc</h7>
   <div id="accordion">
      <h3 id="rent"><a href="#rent"> ...text.... </a></h3>
      <div><p> ...text... </p></div>
   </div></div>
   <div>
   <h7 id="misc2">Misc2</h7>
   <div id="accordion2">
      <h3 id="rent2"><a href="#rent2"> ...text.... </a></h3>
      <div><p> ...text... </p></div>
      <h3 id="rent3"><a href="#rent3"> ...text.... </a></h3>
      <div><p> ...text... </p></div>
   </div></div>

我尝试将我的第二支手风琴改为:

$( "#accordion2" ).accordion("activate", '<%= Request.QueryString["id"] %>');
});

根据此处的建议:Link to open jQuery Accordion 并且还尝试了该页面上也列出的.accordion(“activate”,window.location.hash),并尝试添加附加功能:

$(document).ready(function () {
location.hash && $(location.hash).active('true');

我也试过这里给出的例子:https://forum.jquery.com/topic/accordion-open-a-specific-tab-with-link-from-another-page。这不仅对我不起作用,而且还取消了我在第二支手风琴上的第二次降落时的手风琴格式。

我对jQuery很新,所以解决方案可能非常简单,但我已经尝试了几件事(我刚刚列出了上面的几个,因为那些是我记得的)。

我也使用:site.com/page/one.html#rent2链接到下拉列表,以防万一出现问题。任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

好的,我能够弄清楚对我有用的东西。对于原来的$(function(),用手风琴,我补充道:

   `var hash = window.location.hash;
    var anchor = $('a[href$="'+hash+'"]');
    if (anchor.length > 0){
        anchor.click();
   }`  

然后我的html添加了:

  `<h3><a href="#rent" id="rent">...text...</a></h3>`

这完全符合我的需要,我不记得我在哪里找到了它的链接......