简体   繁体   English

在流星中设置一个带帮助者的类

[英]Set a class with helpers in meteor

im doing a webapp with meteor and i would like to apply a class of active in an li element if the value of it is equal to a variable from a Session variable called ('month') 即时通过meteor进行webapp我想在li元素中应用一个active类,如果它的值等于一个名为('month')的Session变量的变量

 <template name="Navbar"> <ul class="nav navbar-nav"> <li class="{{active}}"><a href="#myPage">Janvier</a></li> <li class="{{active}}"><a href="#">Février</a></li> <li class="{{active}}"><a href="#">Mars</a></li> <li class="{{active}}"><a href="#">Avril</a></li> <li class="{{active}}"><a href="#">Mai</a></li> <li class="{{active}}"><a href="#">Juin</a></li> <li class="{{active}}"><a href="#">Juillet</a></li> <li class="{{active}}"><a href="#">Août</a></li> <li class="{{active}}"><a href="#">Septembre</a></li> <li class="{{active}}"><a href="#">Octobre</a></li> <li class="{{active}}"><a href="#">Novembre</a></li> <li class="{{active}}"><a href="#">Décembre</a></li> </ul> </template> 

My variable session gives me a month and i would like with a helper to put the class "active" to it if the innerHtml is equal to the session variable. 我的变量会话给了我一个月,如果innerHtml等于会话变量,我希望有一个帮助器将类“激活”。 I don't know how to retrieve the value of "this" innerHtml. 我不知道如何检索“this”innerHtml的值。

 Template.Navbar.helpers({ active: function() { if(Session.get("month") === this.value) { return "active"; } } }); 

Thank u for the help . 谢谢你的帮助。

Yoann 约恩

You can pass a variable into your helper class and compare with your Session variable. 您可以将变量传递给助手类,并与Session变量进行比较。

<template name="Navbar">    
  <ul class="nav navbar-nav">
    <li class="{{active 'Janvier'}}"><a href="#myPage">Janvier</a></li>
     ...
  </ul>
</template>

In your Template you can use the variable. 在您的模板中,您可以使用该变量。

Template.Navbar.helpers({
      active: function(month) {
        if(Session.get("month") === month) {
          return "active";
          }
      }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM