加载到不同页面时记住 li 活动状态
Remember li active state when loading to different pages?
我在下面有我的代码:
<ul id="profileList" class="nav nav-list">
<li><a href="<?php echo base_url('user/signature')?>">修改个人签名档</a></li>
<li><a href="<?php echo base_url('user/location')?>">修改个人居住地</a></li>
<li><a href="<?php echo base_url('user/education')?>">修改个人学校专业</a></li>
</ul>
另外这是JS代码:
<script type="text/javascript">
$(document).ready(function() {
// store url for current page as global variable
current_page = document.location.href
// apply selected states depending on current page
if (current_page.index(/signature/)) {
$("ul#profileList li:eq(0)").addClass('active');
} else if (current_page.match(/location/)) {
$("ul#profileList li:eq(1)").addClass('active');
} else if (current_page.match(/education/)) {
$("ul#profileList li:eq(2)").addClass('active');
} else { // don't mark any nav links as selected
$("ul#profileList li").removeClass('active');
};
});
</script>
当我单击第二个和第三个 li 项时,它们运行良好。但是当我单击第一项时,项目未变为活动状态。有什么问题,为什么?
in
if (current_page.index(/signature/)) {
更改为
if (current_page.match(/signature/)) {
据我所知,String.prototype.index
不存在。也许您想使用indexOf
方法。
if (current_page.indexOf('signature') !== -1) {}
另外,当您只想知道是否有匹配项时,不要使用 String.prototype.match
函数,请使用 RegExp.prototype.test
函数。
if (/education/.test('education')) { /*matches*/ }
但是,在您的情况下,您可以使用 match
方法,而不是丢弃匹配项,而是使用它对你有利:
var sections = ['signature', 'location', 'education'],
match = document.location.href.match(new RegExp(sections.join('|'), 'i')),
selectorSuffix = match? ':eq(' + sections.indexOf(match[0].toLowerCase()) + ')' : '';
$('ul#profileList li' + selectorSuffix)[(match? 'add' : 'remove') + 'Class']('active');
相关文章:
- 在Angular 2中布线期间保持零部件处于活动状态
- 如何使bxslider仅在移动视图中处于活动状态
- 悬停下拉菜单即使在鼠标移出后也保持活动状态
- 使用纯javascript而非jquery使所选选项卡处于活动状态并保持非活动状态
- 我如何为列出的选项卡元素编写一个Protractor测试,它会检查它是否's是否处于活动状态
- 返回上一页时,Javascript仍处于活动状态
- Angular+bootstrap ui,检查当前选项卡是否已处于活动状态
- 按钮处于活动状态,焦点无法在Mac FireFox上工作
- 禁用永久活动状态
- 单击我网站中的链接时打开新选项卡,保持当前选项卡处于活动状态
- 网页在等待 AJAX 响应时变为非活动状态
- 广告拦截处于活动状态,然后不要打开超链接到新标签
- 如何创建至少 1 个框处于活动状态的复选框列表
- 如何使按钮具有 :活动状态 1 秒
- JQuery UI 1.11 将选项卡设置为活动状态
- 如何获取链接以保持活动状态
- 限制 .live() 函数在 JavaScript 中变为活动状态的次数
- 加载到不同页面时记住 li 活动状态
- 当子链接处于活动状态时,将活动类添加到父li
- 当子菜单处于活动状态时,将类添加到父级li