将“active”类添加到topmenu,magento 1.9

时间:2017-07-02 11:44:31

标签: magento magento-1.9

我需要在我的topmenu中添加活动类。我已经在论坛上看到了一堆主题,但我没有看到像我这样的代码。你能不能请我帮忙。我试图“echo $ currentUrl”,但这就是我所看到的“localhost.com/app/etc/local.xml”

<?php $_menu = $this->getHtml('level-top') ?>

<?php if($_menu): ?>

<?php
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$baseUrl = Mage::getBaseUrl();

$locale = Mage::app()->getLocale()->getLocaleCode();

$_topMenuItems['ru_RU'] = array(
    /*$this->__('Home') => $baseUrl,*/
    $this->__('IIW, Delivery and payment') => $baseUrl . 'dostavka-oplata.html',
    $this->__('IIW, About us') => $baseUrl . 'pro-nas-kategorija.html',
    $this->__('IIW, Partners') => $baseUrl . 'partneram.html',
    $this->__('IIW, Contacts') => $baseUrl . 'contacts',
    $this->__('IIW, It is interesting') => $baseUrl . 'korisna-informacija.html',
    /* $this->__('IIW, Where to buy') => $baseUrl . 'de-prudbatu.html', */
);
?>

<nav id="nav">
    <ol class="nav-primary">
        <?php
        $i = 0;
        $length = count($_topMenuItems[$locale]);
        foreach ($_topMenuItems[$locale] as $label => $_itemUrl) {
            if ($i == 0) {
                echo $_menu;
            }

            $customMenuClass = ' cm-' . ($i + 1);

            if ($i == 0) {
                $customMenuClass .= ' first';
            } elseif ($i == ($length - 1)) {
                $customMenuClass .= ' last';
            }
            if (trim($currentUrl, '/') == trim($_itemUrl, '/')) {
                $customMenuClass .= ' active';
            }
            ?>

            <li class="level0<?php echo $customMenuClass ?>">
                <a href="<?php echo $_itemUrl ?>" class="level0 "><?php echo $label ?></a>
            </li>
            <?php
            $i++;
        }
        ?>
    </ol>
</nav>

1 个答案:

答案 0 :(得分:0)

<script>
jQuery(document).ready(function($){
var locationUrl = location.href;
$(".yourmenu a.yourclass").each(function() {
  var menuLinkHref = $(this).attr("href");
  if (~locationUrl.indexOf(menuLinkHref)) {
   $(this).addClass("active");
  }
});
});
</script>
如果需要,

JS解决方案 .yourmenu - 菜单类,您的链接必须更改类.actve; .yourclass - 类已经用于菜单中的每个链接(例如“。menu_item_link”)。