如何根据URL在EJS中动态更改一个类。

huangapple go评论83阅读模式
英文:

How to dynamic change a class on EJS based on the URL

问题

我正在学习EJS,我发现我可以在一个文件中制作我的侧边栏并在我的页面上使用它。

但现在我不知道如何自动更改侧边栏上我的锚点的“active”类,有没有一种动态更改它的方式?我的URL根据我所在的页面而变化,所以我想根据那个来更改我的锚点的类。

侧边栏.ejs代码:

<div class="sidebar">
    <a href="/vendor" class="active">All Vendors</a>
    <a href="/expiring">Close to Expire</a>
</div>

我正在使用EJS、NODE.JS和express...

对于任何英语错误,我很抱歉,这不是我的母语。

英文:

I'm learning EJS and I discover that I can do my sidebar in only one file and use it on my pages.

But now I don't know how to automatically change the "active" class on my anchors on the sidebar, there is a way to change it dynamically? My URL changes depending on the page i'm in, so I thought that I can change the class of my anchor based on that.

sidebar.ejs code:

&lt;div class=&quot;sidebar&quot;&gt;
    &lt;a href=&quot;/vendor&quot; class=&quot;active&quot;&gt;All Vendors&lt;/a&gt;
    &lt;a href=&quot;/expiring&quot;&gt;Close to Expire&lt;/a&gt;
&lt;/div&gt;

I'm using EJS, NODE.JS and express...

Sorry for any English mistakes, it's not my mother language

答案1

得分: 1

以下是翻译好的内容:

只需尝试如下示例,

    #sidebar.js

    &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;style&gt;
    	.active &gt; a {
        	color: red;
            text-decoration: none;
            cursor: pointer;
        }
    &lt;/style&gt;
    &lt;script&gt;
    
      var global_queryurl = window.location.href; 
      var url_pathfinder = new URL(global_queryurl);
      var global_queryurl_path = url_pathfinder.pathname; 
      var global_queryurl_spliter = global_queryurl_path.split(&#39;/&#39;);
      var global_pagename_val = global_queryurl_spliter[global_queryurl_spliter.length-1];
    
      $(document).ready(function(){
          $(&quot;.tab&quot;).removeClass(&quot;active&quot;);
          if(global_pagename_val == &quot;vendor&quot;) {
            $(&quot;#1&quot;).addClass(&quot;active&quot;);   
          } else if(global_pagename_val == &quot;expiring&quot;) {
            $(&quot;#2&quot;).addClass(&quot;active&quot;);   
          } else {
            $(&quot;#1&quot;).addClass(&quot;active&quot;);   
          }
      });
    &lt;/script&gt;
    
    &lt;!--sidebar-menu--&gt;
    &lt;div id=&quot;sidebar&quot;&gt;
      &lt;ul&gt;
      &lt;li class=&quot;tab&quot; id=&quot;1&quot; &gt;&lt;a href=&quot;/vendor&quot; &gt;Vendor&lt;/a&gt; &lt;/li&gt;
      &lt;li class=&quot;tab&quot; id=&quot;2&quot; &gt;&lt;a href=&quot;/expiring&quot; &gt;Expiring&lt;/a&gt; &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
    &lt;!--sidebar-menu--&gt;

(Note: This is the same content as before, with only the code portions translated.)

英文:

Just try like this example,

#sidebar.js

&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;style&gt;
	.active &gt; a {
    	color: red;
        text-decoration: none;
        cursor: pointer;
    }
&lt;/style&gt;
&lt;script&gt;

  var global_queryurl = window.location.href; 
  var url_pathfinder = new URL(global_queryurl);
  var global_queryurl_path = url_pathfinder.pathname; 
  var global_queryurl_spliter = global_queryurl_path.split(&#39;/&#39;);
  var global_pagename_val = global_queryurl_spliter[global_queryurl_spliter.length-1];

  $(document).ready(function(){
      $(&quot;.tab&quot;).removeClass(&quot;active&quot;);
      if(global_pagename_val == &quot;vendor&quot;) {
        $(&quot;#1&quot;).addClass(&quot;active&quot;);   
      } else if(global_pagename_val == &quot;expiring&quot;) {
        $(&quot;#2&quot;).addClass(&quot;active&quot;);   
      } else {
        $(&quot;#1&quot;).addClass(&quot;active&quot;);   
      }
  });
&lt;/script&gt;

&lt;!--sidebar-menu--&gt;
&lt;div id=&quot;sidebar&quot;&gt;
  &lt;ul&gt;
  &lt;li class=&quot;tab&quot; id=&quot;1&quot; &gt;&lt;a href=&quot;/vendor&quot; &gt;Vendor&lt;/a&gt; &lt;/li&gt;
  &lt;li class=&quot;tab&quot; id=&quot;2&quot; &gt;&lt;a href=&quot;/expiring&quot; &gt;Expiring&lt;/a&gt; &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;!--sidebar-menu--&gt;

huangapple
  • 本文由 发表于 2020年1月3日 21:40:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定