3

由于我对 jQuery 和 PHP 的了解很少,我几乎完成了一个手风琴式菜单,当活动页面打开时,链接显示为“活动”,而在活动页面上,滑块菜单在正确的部分保持打开状态。

我在底部链接中添加了一些 php。子链接在活动页面上显示为活动。然而,父元素不会,也会自行关闭。

我真的很感激任何帮助!!!

这是我的代码:

<ul id="accordion">    
<ul class="parent">
    <li>
        <a href="#" class="slide">CMS</a>

        <ul class="child">
            <li><a href="/learn/intro-to-cms">Intro to CMS</a></li>
            <li><a href="/learn/specific-cms">Specific CMS</a></li> 
            <li><a href="/learn/installing-a-cms">Installing a CMS</a></li> 
        </ul>

     </li>
</ul>

<ul class="parent">
        <li>
            <?php
            if ($this_page=="Customising-Google-Forms" || $this_page=="Web-Event-Notes"){
            echo "<a href=\"#\" class=\"slide current\">Other</a>";
            }else{
            echo "<a href=\"#\" class=\"slide\">Other</a>";
            }
            ?>

        <ul class="child">
            <?php
            if ($this_page=="Customising-Google-Forms"){
            echo "<li class=\"current\"><a href=\"/learn/customising-google-forms\">Customising-google-forms</a></li>";
            }else{
            echo "<li><a href=\"/learn/customising-google-forms\">Customising Google Forms</a></li>";
            }
            ?>
            <?php
            if ($this_page=="Web-Event-Notes"){
            echo "<li class=\"current\"><a href=\"/learn/web-event-notes\">Web Event Notes</a></li>";
            }else{
            echo "<li><a href=\"/learn/web-event-notes<\">Web-Event-Notes</a></li>";
            }
            ?>
        </ul>

     </li>
</ul>

<script type="text/javascript">
$(function(){
// hide all links on load
$('ul.child').hide();
// for image
// $("a.slide:first").css("background-image","url('path')");

$('ul.parent a.slide').click(function(){
    $('ul.parent a.slide').css("color","#000");

    $('ul.parent a.slide').hover(function(){
        $(this).css("color","#ef492f");
    });

    $('ul.parent a.slide').mouseout(function(){
        $(this).css("color","#000");
    });

    $(this).mouseout(function(){
        $(this).css("color","#000");
    });

    $(this).css("color","#000");


    // slide all up
    $('ul.child').slideUp('slow');

    // show the links of current heading
    $(this).next().find('a').show();

    // slide down current heading
    $(this).next().slideDown('fast');

    // prevent default action
    return false;
});

if($("ul li a").hasClass("current")) {
$(this).closest("ul").slideDown("fast") //open the menu
}

});

</script>
4

1 回答 1

1

在我看来,实现这一点的最佳方法是制作一个包含链接的 PHP 数组,然后从中构建一个 html 菜单。通过这种方式,PHP 可以根据 url 参数控制哪些链接将持有“当前”类。最后一步将是 jquery 来获取“.current”列表项并使其可见。下面的工作可能看起来很多,但是当你得到它时,它将是强大且可重用的:)

//Grab the page parameter somehow.. below = example
$page = $_GET['page'];

//Build menu array containing links and subs
$items = Array( 
    //highest level
    'cms' => Array(
        'title' => 'CMS',
        //Array containing submenu items for cms
        'subs' => Array(
            'intro-to-cms' => Array('title' => 'Intro to CMS'),
            'specific-cms' => Array('title' => 'Specific CMS'),
            'installing-a-cms' => Array('title' => 'Installing a CMS')
        ),
    )
);

//Display the menu
echo navlinks($items, $page);

/**
 * Recursive function creates a navigation out of an array
 * @param type $items
 * @return string containing navigationlist
 */
function navlinks($items, $page=false)
{
  $html = '<ul>';
  foreach ($items AS $uri => $info) {
    //Check if the pagename is the same as the link name and set it to current when it is
    $html .= '<li'.($info['title'] == $page ? ' class="current"' : '').'>';
    echo '  <a href="' . $uri . '">' . $info['title'] . '</a>';
    //If the link has a sub array, recurse this function to build another list in this listitem
    if (isset($info['subs']) && is_array($info['subs'])) {
      $html .= navlinks($info['subs']);
    }
    $html .= '</li>';
  }
  $html .= '</ul>';
  return $html;
}
于 2012-09-24T00:51:17.863 回答