获取当前URL路径并指定“active”并从foreach laravel添加类

时间:2018-05-24 05:09:14

标签: javascript jquery html

i want like this 为什么课程提醒警报 - 成功无法添加标签href?

<ul class="nav nav-pills" id="nav">
   @foreach($menu as $d)
   <li role="presentation"><a class="text-capitalize " href="{!! $d->name !!}">{!! $d->name !!}</a></li>
   @endforeach
</ul>
<script type="text/javascript">
   $(document).ready(function() {
   // get current URL path and assign 'active' class
   var pathname = window.location.pathname;
   $('.nav > li > a[href="'+pathname+'"]').parent().addClass('alert alert-success');
   })
</script>

1 个答案:

答案 0 :(得分:0)

尝试使用以下

$('.nav > li > a[href*="'+pathname+'"]').parent().addClass('alert alert-success');