open accordion with external link

做~自己de王妃 提交于 2020-01-14 03:19:28

问题


i am trying to open the the accordion using external link..Actually there are three main accordions on the page(about,results,contact)

and there is a menubar with items(about,results,contact).

what i want is when i click on about menu item it should open the about accordion

the below is jquery code

$(function() {

    $( "#accordion" ).accordion({              
        collapsible: true,
        autoHeight: false , 
        active: false,
        navigation:true,       

    }); 

    $(".exlnk").click(function(event){
           window.location.hash=this.hash;
            });         
});

The below is html menubar code:

    <div id="navmenu" style='z-index:9999;'>
      <ul>
        <li><a href="#anchor_about" class="anchor_about">About</a></li>
        <li><a href="#" >Results</a></li>
       <li><a href="#" class ="anchor_contact">Contact</a></li>
      </ul>
    </div>

and below is jquery code for external link

    <script>

     $(document).ready(function(){

     $('a.anchor_about').click(function(){

     $("#accordion").accordion("activate", '<?php echo $_GET['id']; ?>');

 return false;

  });
   });

   </script>

but its not working..:(

anyone knows about same? thanks in advance

EDIT1

<script>

 $("#navmenu ul").children("li").click(function()
 {
  $("#accordion").accordion("activate", <?php echo (int)$_GET['id']; ?>);
});

EDIT2

Below is the accordion code

        <div class="demo">                      

            <div id="accordion">                                                       <!--- main accordion 1 demo-->
                <h3><a href="#anchor_home"></a></h3>                            
                    <div id="accordion1" style="margin-bottom:17px;"  >
                        <h1><a href="#" ></a></h1>
                            <div id="content_our_offer" >                              <!--- sub accordion 1 demo-->
                                <?php $page_id=1742;?>                      
                                <?php get_page( $page_id ) ;
                                $page_data = get_page( $page_id );
                                echo '<h3>'. $page_data->post_title .'</h3>';// echo the title
                                echo apply_filters('the_content', $page_data->post_content); ?>              
                            </div>

                    <div style="margin-top:20px;">                                   <!--- sub accordion 2 demo-->
                        <h2  ><a href="#" ></a></h2>    </div>          
                            <div id="content_our_offer" >
                                <?php $page_id=1742;?>                      
                                <?php get_page( $page_id ) ;
                                $page_data = get_page( $page_id );
                                echo '<h3>'. $page_data->post_title .'</h3>';// echo the title
                                echo apply_filters('the_content', $page_data->post_content); ?>              
                            </div>

            </div>


            <div style="margin-top:20px;">
            <h6><a class="exlnk" href="#about" title="About" ></a></h6>   </div>                                              <!--- main accordion 2 demo-->     
                <div id="id_ourresults">
                    <?php
                        $directory = 'our_results'; 
                        try {    
                            // Styling for images

                            foreach ( new DirectoryIterator("../" . $directory) as $item ) {            
                                if ($item->isFile()) {
                                    echo "<div class=\"expand_image\">";
                                    $path = "/" . $directory . "/" . $item;
                                    echo "<img src=\"" . $path . "\" width=861 height=443  />";
                                    echo "</div>";
                                    }
                                }
                            }
                            catch(Exception $e) {
                            echo 'No images found for this player.<br />';
                            }


       ?>




                </div>          
                <div style="margin-top:20px;">


            <h4><a href="#anchor_contact"></a></h4> </div>                                          <!--- main accordion 3 demo-->
                <div id="id_contactus">      
                    <?php $page_id=1791 ;?>
                    <?php get_page( $page_id ) ;
                    $page_data = get_page( $page_id );
                    echo '<h3>'. $page_data->post_title .'</h3>';// echo the title
                    echo apply_filters('the_content', $page_data->post_content); ?>              
                </div>        

            </div>

    </div><!-- End demo -->

回答1:


$('a.anchor_about').click(function(){

   $("#accordion").accordion("activate", '<?php echo $_GET['id']; ?>');

   return false;

});

I don't think $_GET['id'] in the accordion activate line is going to work as I guess you want something like this

$('a.anchor_about').click(function(){
   var sectionId = $(this).attr("href");
   $("#accordion").accordion("activate", sectionId);

   return false;

});

check if this works - if there are any errors let me know.

EDIT **

Also, I think to open accordion you need an index based value not a ID value like #someId (which works for tab for sure). The index is zero-based so that first section of accordion can be activated by passing a zero value, second with 1, and so on.




回答2:


Give the menu items a name according to index

<div id="navmenu" style='z-index:9999;'>
    <ul>
        <li><a href="#" name="0">About</a></li>
        <li><a href="#" name="1">Results</a></li>
        <li><a href="#" name="2">Contact</a></li>
    </ul>
</div>

then activate the appropriate accordionitem with the index given in the menu items:

$("#navmenu ul").children("li").click(function()
{
    $("#accordion").accordion("activate", $(this).attr("name"));

    // ALSO POSSIBLE (name attribute not needed):
    // Only if the menu items are in the same order as the accordion items
    $("#accordion").accordion("activate", $(this).index());
});

Edit

I just figured your code is in a different file, so here's an option to minimize the change to your code. Seeing as you send a hash and GET the ID in a new page. The accordion needs an indexnumber, not an ID: Accordion Methods

<div id="navmenu" style='z-index:9999;'>
    <ul>
        <li><a href="#0">About</a></li>
        <li><a href="#1">Results</a></li>
        <li><a href="#2">Contact</a></li>
    </ul>
</div>



回答3:


$('#accordion').accordion({"active": $(window.location.hash).index()});


来源:https://stackoverflow.com/questions/11683825/open-accordion-with-external-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!