【问题标题】:Select Closest href Match With jQuery?选择与 jQuery 最接近的 href 匹配?
【发布时间】:2021-05-11 04:41:12
【问题描述】:

尝试使用jQuery 过滤器将<a> hrefs 缩小到一个,然后添加标签active 类。
我这样做是因为我从侧边栏子菜单被重定向并且希望仍然保持侧边栏菜单打开。

$(document).ready(function () {
  var url = window.location;


  $('ul.treeview-menu a').filter(function () {
      return url.indexOf(this.href) > -1;
  }).parentsUntil(".sidebar-menu > .treeview-menu").addClass('active');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="sidebar-menu" data-widget="tree">
    <li class="header">General</li>
    <li class="home"><a href="/p/index.php"><i class="fas fa-tachometer-alt"></i> <span> Dashboard</span></a></li>

    <li class="header">Data Managment</li>
    <li class="treeview">
        <a href="#">p <span>Tree</span>
            <span class="pull-right-container">
                <i class="fa fa-angle-left pull-right"></i>
            </span>
        </a>
        <ul class="treeview-menu">
            <li><a href="/p/pages/foo.php">foo</a></li>
            <li><a href="/p/pages/bar.php">bar</a></li>
            <li><a href="/p/pages/blah.php">blah</a></li>
            <li><a href="/p/pages/blahh.php">blahh</a></li>
        </ul>
    </li>
    
    <li class="treeview">
        <a href="#"></i> <span>Tree 2</span>
            <span class="pull-right-container">
                <i class="fa fa-angle-left pull-right"></i>
            </span>
        </a>
        <ul class="treeview-menu">
            <li><a href="/p/pages/aaa.php">aaa</a></li>
            ...
        </ul>
    </li>

</ul>

如果我在foohref 中被重定向,就像/p/pages/foo.php/new.php 我仍然想保持活跃。

为什么这不起作用?

【问题讨论】:

  • @freedomn-m 不幸的是没有用。我会尝试分享一些html
  • @freedomn-m 我在控制台中收到此错误 url.indexOf is not a function.closest(".sidebar-menu").find(".treeview-menu").addClass('active') 返回所有这些错误,但 html 不正确
  • @freedomn-m 控制台错误通过将url 替换为window.location.href 而消失
  • @freedomn-m return console.log(window.location.href.indexOf(this.href) &gt; -1) 返回false。这意味着它找不到任何东西!?
  • @freedomn-m 什么都没有。是空的

标签: javascript html jquery


【解决方案1】:

其他方法是使用each 循环,然后比较url == this.href 如果是,则将类添加到该li 并打开最近的ul

演示代码

$(document).ready(function() {
  //just for demo...
  var url = 'https://stacksnippets.net/p/pages/bar.php';
  $('ul.treeview-menu a').each(function() {
    //if full url is not same with li use indexof else if http://...also there in href of a tag use below..
    if (url == this.href) {
      $(this).addClass('active') //add class active to `a`
      $(this).closest("ul").slideDown(); //slide ul tag where `li` is active
    }
  })
})
.treeview-menu {
  display: none
}

.active {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="sidebar-menu" data-widget="tree">
  <li class="header">General</li>
  <li class="home"><a href="/p/index.php"><i class="fas fa-tachometer-alt"></i> <span> Dashboard</span></a></li>

  <li class="header">Data Managment</li>
  <li class="treeview">
    <a href="#">p <span>Tree</span>
            <span class="pull-right-container">
                <i class="fa fa-angle-left pull-right"></i>
            </span>
        </a>
    <ul class="treeview-menu">
      <li><a href="https://stacksnippets.net/p/pages/foo.php">foo</a></li>
      <li><a href="https://stacksnippets.net/p/pages/bar.php">bar</a></li>
      <li><a href="https://stacksnippets.net/p/pages/blah.php">blah</a></li>
      <li><a href="https://stacksnippets.net/p/pages/blahh.php">blahh</a></li>
    </ul>
  </li>

  <li class="treeview">
    <a href="#"> <span>Tree 2</span>
      <span class="pull-right-container">
                <i class="fa fa-angle-left pull-right"></i>
            </span>
    </a>
    <ul class="treeview-menu">
      <li><a href="https://stacksnippets.net/p/pages/aaa.php">aaa</a></li>
      ...
    </ul>
  </li>

</ul>

更新代码

$(window).on('load', function() {
  var url = window.location.href;
  var new_url = url.slice(url.lastIndexOf('/') + 1);
  $('ul.treeview-menu a').each(function() {
    var res = this.href.replace(".php", "");
    var href = res + "/" + new_url;
    if (href == url) {
      $(this).parent().addClass('active') //add class active to `a
      $(this).closest("ul").slideDown(); //slide ul tag where `li` is active
    }
  })
});

【讨论】:

  • 网址不一样,我已经从子菜单重定向,新的网址在最后有一个新的部分现在你将如何使用indexOf
  • 嗨 :) 我不知道,我无法摆脱这个错误 jQuery.Deferred exception: url.lastIndexOf is not a function TypeError: url.lastIndexOf is not a functionUncaught TypeError: url.lastIndexOf is not a function 对不起我的初学者问题
  • check this post 试试这个建议看看是否可行:)
  • 好的,我确实解决了这个错误。和console.log(new_url) 显示像这样new.php 和if 语句返回空的URL 的最后一部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
相关资源
最近更新 更多