Javascript 基于类似Url的Url添加活动导航类
我正在尝试在导航中添加活动类。 我用了这篇文章。 它工作得很好。 但我的问题是在这种情况下Javascript 基于类似Url的Url添加活动导航类,javascript,jquery,laravel,bootstrap-4,navigation,Javascript,Jquery,Laravel,Bootstrap 4,Navigation,我正在尝试在导航中添加活动类。 我用了这篇文章。 它工作得很好。 但我的问题是在这种情况下 /blog, /blogAds 如果url为/blog,则将活动类添加到两个导航项中。 有什么解决办法吗?试试这个 if( new RegExp(location.pathname + "$").test( $this.attr('href') ) ) { $this.addClass('active'); } 通过以下方式进行测试: new RegExp('\/blog$').test('
/blog, /blogAds
如果url为/blog,则将活动类添加到两个导航项中。
有什么解决办法吗?试试这个
if( new RegExp(location.pathname + "$").test( $this.attr('href') ) ) {
$this.addClass('active');
}
通过以下方式进行测试:
new RegExp('\/blog$').test('/blogAds'); // returns false
new RegExp('\/blog$').test('/blog'); // returns true
new RegExp('\/blog$').test('/blogAds'); // returns false
new RegExp('\/blog$').test('/blog'); // returns true