Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于类似Url的Url添加活动导航类_Javascript_Jquery_Laravel_Bootstrap 4_Navigation - Fatal编程技术网

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