有没有更干净的方法来执行每个页面的侧边栏活动状态?

发布于 2024-11-24 21:21:57 字数 2026 浏览 1 评论 0原文

我有这个侧边栏代码

    <li><a href="/restaurant_pos"><span>Restaurant POS Systems</span></a>
<ul>
    <li><a href="/restaurant_pos">Restaurant POS Systems</a></li>
    <li><a href="/bar_nightclub_pos">Bar and Nightclub POS Systems</a></li>
    <li><a href="/bbq-restaurant-pos">BBQ Restaurant POS</a></li>
    <li><a href="/bowling-alley-pos-system">Bowling Alley Point of Sale</a></li>
    <li><a href="/cafe-pos">Cafe Point of Sale Systems</a></li>
    <li><a href="/chinese-restaurant-pos">Chinese Restaurant POS Systems</a></li>
    <li><a href="/fine-dining-pos">Fine Dining Point of Sale Systems</a></li>
    ....
    ....

,我想向当前页面添加一个活动类,所以这就是我采取的方法

    <li><a href="/restaurant_pos"><span>Restaurant POS Systems</span></a>
<ul>
    <li><a <?php if($_SERVER['REQUEST_URI'] == '/restaurant_pos/'){
        echo "class='active' ";
    } 
    ?> href="/restaurant_pos">Restaurant POS Systems</a></li>
    <li><a 
        <?php if($_SERVER['REQUEST_URI'] == '/bar_nightclub_pos/'){
            echo "class='active' ";
        } 
        ?>
        href="/bar_nightclub_pos">Bar and Nightclub POS Systems</a></li>
    <li><a href="/bbq-restaurant-pos">BBQ Restaurant POS</a></li>
    <li><a href="/bowling-alley-pos-system">Bowling Alley Point of Sale</a></li>
    <li><a href="/cafe-pos">Cafe Point of Sale Systems</a></li>
    <li><a href="/chinese-restaurant-pos">Chinese Restaurant POS Systems</a></li>
    <li><a href="/fine-dining-pos">Fine Dining Point of Sale Systems</a></li>
    ....
    ....

,但我觉得必须有一个更好、更干净的方法来做到这一点......任何想法

I have this sidebar code

    <li><a href="/restaurant_pos"><span>Restaurant POS Systems</span></a>
<ul>
    <li><a href="/restaurant_pos">Restaurant POS Systems</a></li>
    <li><a href="/bar_nightclub_pos">Bar and Nightclub POS Systems</a></li>
    <li><a href="/bbq-restaurant-pos">BBQ Restaurant POS</a></li>
    <li><a href="/bowling-alley-pos-system">Bowling Alley Point of Sale</a></li>
    <li><a href="/cafe-pos">Cafe Point of Sale Systems</a></li>
    <li><a href="/chinese-restaurant-pos">Chinese Restaurant POS Systems</a></li>
    <li><a href="/fine-dining-pos">Fine Dining Point of Sale Systems</a></li>
    ....
    ....

ans I want to add an active class to the current page, so this is the approach i took

    <li><a href="/restaurant_pos"><span>Restaurant POS Systems</span></a>
<ul>
    <li><a <?php if($_SERVER['REQUEST_URI'] == '/restaurant_pos/'){
        echo "class='active' ";
    } 
    ?> href="/restaurant_pos">Restaurant POS Systems</a></li>
    <li><a 
        <?php if($_SERVER['REQUEST_URI'] == '/bar_nightclub_pos/'){
            echo "class='active' ";
        } 
        ?>
        href="/bar_nightclub_pos">Bar and Nightclub POS Systems</a></li>
    <li><a href="/bbq-restaurant-pos">BBQ Restaurant POS</a></li>
    <li><a href="/bowling-alley-pos-system">Bowling Alley Point of Sale</a></li>
    <li><a href="/cafe-pos">Cafe Point of Sale Systems</a></li>
    <li><a href="/chinese-restaurant-pos">Chinese Restaurant POS Systems</a></li>
    <li><a href="/fine-dining-pos">Fine Dining Point of Sale Systems</a></li>
    ....
    ....

But i feel there has got to be a better and cleaner way to do this...any ideas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

简单 2024-12-01 21:21:57

当然,

  1. 将所有链接及其文本放入一个数组中。

  2. 然后迭代数组以进行输出。

  3. 如果输出就像您知道的那样,没有活动的功能,您可以继续下一步。

  4. 引入对当前映射链接的请求的检查,如果是,则将类添加到输出。

您已经使用迭代解决了您的问题。通过将数据放入可迭代(可循环)结构中,可以将相同的方法应用于相同的数据。不要重复代码,重复数据。希望这有帮助。

Sure,

  1. put all the links and their texts inside an array.

  2. Then iterate over the array to do the output.

  3. If the output is like you know it w/o the active functionality, you can continue with the next step.

  4. Introduce the checking for the current request mapping a link and if it does, add the class to the output.

You've solved your problem using iteration. The same method is applied to the same data, by putting the data into an iterate-able (loopable) structure. Don't repeat the code, repeat the data. Hope this helps.

自此以后,行同陌路 2024-12-01 21:21:57

在 JavaScript/jQuery 中这可能更容易做到。将交互保持在客户端也将节省服务器请求(尽管这在负载较高的站点上可能更值得关注)。

像这样的事情 - 您可能需要根据站点的 URL 调整 url 数组位置:

jsfiddle: http://jsfiddle.net/g_thom/AXm6e/2/

var pathname = window.location.href;
var partsArray = pathname.split('/');
var url = '/' + partsArray[3];
var a_s = $('li a');
a_s.removeClass('active');
a_s.each(function () {
   if ($(this).attr('href') == url) {
       $(this).addClass('active');
   } 
}); 

This might be easier to do in JavaScript/jQuery. Keeping the interaction client-side will save on server requests, too (though this may be more of a concern on sites with higher loads).

Something like this - you may have to adjust the url array position based on your site's URL:

jsfiddle: http://jsfiddle.net/g_thom/AXm6e/2/

var pathname = window.location.href;
var partsArray = pathname.split('/');
var url = '/' + partsArray[3];
var a_s = $('li a');
a_s.removeClass('active');
a_s.each(function () {
   if ($(this).attr('href') == url) {
       $(this).addClass('active');
   } 
}); 
白色秋天 2024-12-01 21:21:57

在 foreach 循环中循环遍历数据数组

<?php
$links = array(
    array('/restaurant_pos','Restaurant POS Systems'),
    array('/bar_nightclub_pos/','Bar and Nightclub POS Systems')
);
foreach($links as $k => $v){
    if($_SERVER['REQUEST_URI'] == '/bar_nightclub_pos/'){ $class = ' class="active"'; } else { $class = ''; }
    echo '<li><a href="'.$v[0].'"'.$class.'>'.$v[1].'</a></li>';
};
?>

输出:

Loop through data array in foreach loop

<?php
$links = array(
    array('/restaurant_pos','Restaurant POS Systems'),
    array('/bar_nightclub_pos/','Bar and Nightclub POS Systems')
);
foreach($links as $k => $v){
    if($_SERVER['REQUEST_URI'] == '/bar_nightclub_pos/'){ $class = ' class="active"'; } else { $class = ''; }
    echo '<li><a href="'.$v[0].'"'.$class.'>'.$v[1].'</a></li>';
};
?>

Outputs:

醉酒的小男人 2024-12-01 21:21:57

我会做这样的事情:

<?php
$restaurants = array(
    array('label' => 'Restaurant POS Systems', 'url' => '/restaurant_pos'),
    array('label' => 'Bar and Nightclub POS Systems', 'url' => '/bar_nightclub_pos'),
    ...
);

/*set active restaurant*/
foreach ($restaurants as &$r) {
    $r['active'] = ($_SERVER['REQUEST_URI'] == $r['url'])? 'active': '';
}
unset($r);
?>
<ul>
<?php foreach ($restaurants as $r) { ?>
    <li><a href="<?php echo $r['url']; ?>" class="<?php echo $r['active']; ?>"><?php echo $r['label']; ?></a></li>
<?php } ?>
</ul>

在生成 HTML 的代码部分中使用尽可能少的编程逻辑通常是一个好主意。

I would do something like this:

<?php
$restaurants = array(
    array('label' => 'Restaurant POS Systems', 'url' => '/restaurant_pos'),
    array('label' => 'Bar and Nightclub POS Systems', 'url' => '/bar_nightclub_pos'),
    ...
);

/*set active restaurant*/
foreach ($restaurants as &$r) {
    $r['active'] = ($_SERVER['REQUEST_URI'] == $r['url'])? 'active': '';
}
unset($r);
?>
<ul>
<?php foreach ($restaurants as $r) { ?>
    <li><a href="<?php echo $r['url']; ?>" class="<?php echo $r['active']; ?>"><?php echo $r['label']; ?></a></li>
<?php } ?>
</ul>

It is generally a good idea to have as little programming logic as possible in the part of the code that generates the HTML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文