有没有更干净的方法来执行每个页面的侧边栏活动状态?
我有这个侧边栏代码
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,
将所有链接及其文本放入一个数组中。
然后迭代数组以进行输出。
如果输出就像您知道的那样,没有活动的功能,您可以继续下一步。
引入对当前映射链接的请求的检查,如果是,则将类添加到输出。
您已经使用迭代解决了您的问题。通过将数据放入可迭代(可循环)结构中,可以将相同的方法应用于相同的数据。不要重复代码,重复数据。希望这有帮助。
Sure,
put all the links and their texts inside an array.
Then iterate over the array to do the output.
If the output is like you know it w/o the active functionality, you can continue with the next step.
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.
在 JavaScript/jQuery 中这可能更容易做到。将交互保持在客户端也将节省服务器请求(尽管这在负载较高的站点上可能更值得关注)。
像这样的事情 - 您可能需要根据站点的 URL 调整
url
数组位置:jsfiddle: http://jsfiddle.net/g_thom/AXm6e/2/
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/
在 foreach 循环中循环遍历数据数组
输出:
Loop through data array in foreach loop
Outputs:
我会做这样的事情:
在生成 HTML 的代码部分中使用尽可能少的编程逻辑通常是一个好主意。
I would do something like this:
It is generally a good idea to have as little programming logic as possible in the part of the code that generates the HTML.