简体   繁体   English

重新加载时按钮颜色不会改变

[英]Button colour is not changing on reload

By default the current date button (the first button) should display in blue.默认情况下,当前日期按钮(第一个按钮)应显示为蓝色。 When it's clicked other buttons should have a grey color and the current button should have a blue color.当它被点击时,其他按钮应该是灰色的,当前按钮应该是蓝色的。

It is working for the first time when rendered and after a reload the color of the button is not working.它在渲染时第一次工作,并且在重新加载后按钮的颜色不起作用。

Template.agendaPage.helpers({
    'getDateFilters': function() {
      var allDays = Session.get('allDays')
      allDays = allDays.map(function(val) {
        return moment(val).format("MMM DD")
      })
      return allDays;
    },
  })

  Template.agendaPage.events({
    'click .dateFilter': function(event) {
      var bgclr;
      var id = $(event.target).attr('data-index');
      var allDays = Session.get('allDays');
      Session.set('currentDay', allDays[id]);
      $('.dateFilter').css({
        "background-color": "#575757"
      });
      $(`.dateFilter[data-index=${id}]`).css({
        "background-color": "#0091FF"
      });
    },
  })

  Template.agendaPage.rendered = function() {
    var _this = this;
    Meteor.setTimeout(function() {
      var dayObj = {}
      var days = []
      var startDay = ''
      var eventId = Session.get('data-event-id');
      var orgId = Session.get('data-organisation-id');
      var sessions = Content.find({
        eventId: eventId,
        orgId: orgId
      }, {
        sort: {
          "sessionStartTime": 1
        }
      }).fetch();
      var eventData = Events.findOne({
        "eventId": eventId
      })
      sessions.forEach(function(content, index, sessionArray) {
        if (content.sessionStartTime) {
          var startime = content.sessionStartTime.toString()
          var dayformat = moment(startime).format("dddd, MMM DD YYYY")
          if (dayObj.hasOwnProperty(dayformat)) {
            dayObj[dayformat].push(content)
          } else {
            dayObj[dayformat] = []
            dayObj[dayformat].push(content)
          }
          if (!startDay) {
            startDay = dayformat
          }
          if (days.indexOf(dayformat) == -1) {
            days.push(dayformat)
          }
        }
      })
      Session.set('dayObj', dayObj)
      Session.set('allDays', days)
      Session.set('currentDay', startDay)
      Session.set("track", "all")
      Session.set("trackDes", "all")
      var allDates = Object.keys(Session.get('dayObj'))
      if (allDates.length == 1) {
        $('.next-day').attr('disabled', "")
      } **
      $('.dateFilter[data-index=0]').css({
        "background-color": "#0091FF"
      }); **
    }, 300)
    Meteor.setTimeout(function() {
      if ($('.eventBanner').length == 0) {
        $('.agedaTitle').css("margin-top", "100px")
      }
    }, 1000)
  }
{{#if getDateFilters}} 
  {{#each getDateFilters}}
    <button type="button" data-index="{{@index}}" class="btn btn-primary btn-sm dateFilter" style="border-radius: 12.5px;font-size: 12px;width: 99px;font-family: TTCommons-Regular;letter-spacing: 0px;font-weight: bold;height: 25px;background-color: #575757;line-height: 14px;float: left;margin-left: 20px;outline: none;border: none;">{{this}}</button>
  {{/each}} 
{{/if}}

Thank you!谢谢!

You should use a reactive variable (ReactiveVar, see the docs here ) to store the currentDay instead of Session.您应该使用反应变量(ReactiveVar,请参阅此处的文档)来存储 currentDay 而不是 Session。 ReactiveVar are much more powerfull and reliable than Session, especially because you create them on the onCreated function of a Template instance. ReactiveVar 比 Session 更强大和更可靠,特别是因为您在模板实例的onCreated function 上创建它们。

I advise you to replace all use of Session with ReactiveVar.我建议你用 ReactiveVar 替换所有使用 Session。

Here is a code sample for the currentDay variable:这是 currentDay 变量的代码示例:

Template.agendaPage.events({
    'click .dateFilter': function(event) {
         var bgclr;
         var id = $(event.target).attr('data-index');
         var allDays = Session.get('allDays');
         Template.instance().currentDay.set(allDays[id]);
     });
});
Template.agendaPage.helpers({
    currentDay : function(){
        return Template.instance().currentDay.get()
    }
});


Template.agendaPage.onCreated(function(){
    this.currentDay = new ReactiveVar(null);
});

Also, based on this, you should use an external css with css classes containing your colors (ie .active class) and use {{#if}} statement to set the class to the html element that is getting activated ( <button> ) Also, based on this, you should use an external css with css classes containing your colors (ie .active class) and use {{#if}} statement to set the class to the html element that is getting activated ( <button> )

Template.agendaPage.helpers({
    isActive : function(element){
        let currentDay = Template.instance().currentDay.get()
        // do wathever logic you need based on the `element` variable passed as param
        return true; // or false
    }
});

<button class="{{#if (isActive this)}}active{{/if}}">

.active{
    "background-color": "#0091FF"
}

I haven't tested the sample codes, don't hesitate to comment if you need more details我还没有测试示例代码,如果您需要更多详细信息,请随时发表评论

JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div> - JavaScript for changing a <div> colour from a button press

暂无
暂无

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

相关问题 按钮颜色不变 - Button colour not changing 使用 CSS 更改按钮颜色 - Changing button colour using CSS 更改按钮的背景颜色(开/关) - Changing the background colour of a button (On/Off) 使用按钮java脚本更改某些内容的颜色 - Changing the colour of something using button java script Java语言边框颜色在单选按钮单击时未更改 - Javascript border colour not changing on radio button click 使用 React 中的按钮单击更改 div 颜色样式 - Changing div colour styling with button click in React JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div> - JavaScript for changing a <div> colour from a button press React/Redux 使用状态更改嵌套数组中按钮的背景颜色 - React/Redux Changing background colour of button in nested array using state 单击按钮时更改 Reactjs 中特定元素的颜色 - Changing the colour of specific elements in Reactjs when clicking a button 使用上一个/下一个按钮时更改活动标签的颜色 - Changing active tab colour when using a previous/next button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM