【问题标题】:adding a class to a calendar day in jQuery UI (MultipleDates)在 jQuery UI (MultipleDates) 中向日历日添加类
【发布时间】:2017-11-17 04:23:22
【问题描述】:

我正在为 jQuery UI 日期选择器使用多个日期选择器。直到现在一切都很顺利。事实证明,制作它的开发人员已经多年没有触及它,并且预先突出显示日期不再有效。所以这是我的计划。我将我需要突出显示的日期放入一个数组中,然后使用 jQuery 将必要的类添加到日历中。这就是我卡住的地方。当它看起来像这样时,我如何定位这个特定的锚:

<td class=" ui-datepicker-week-end  ui-datepicker-current-day" data-handler="selectDay" data-event="click" data-month="10" data-year="2017">
<a class="ui-state-default" href="#">18</a> <!-- needs class: ui-state-active-->
</td>

那么我如何根据数据月、数据年以及锚点中的数字进行定位?非常感谢所有帮助!

【问题讨论】:

    标签: javascript jquery css jquery-ui


    【解决方案1】:

    【讨论】:

    • 我会试一试。理想情况下,我想使用插件使用的相同类,这样我就可以模拟它被点击了,至少以编程方式
    • 这与课程无关。它是这个 jQuery 插件的本机回调,你不需要任何类型的选择器,因为这将是第三方的所有后果:复杂的依赖关系和禁止的更新。一切都很简单,比如:你能构建选择器吗?是的。你需要一个吗?不,这是不好的编码习惯。
    【解决方案2】:

    要获取锚标记内的值,您可以使用 jQuery 选择器使用月份和年份来缩小搜索范围。

    然后遍历该月和该年的所有日子,并使用锚点上的 find() 方法来获取日期。将日历日期与数组中的日期进行比较,如果匹配,则突出显示它

    这是一个示例,但我不确定性能或它将如何扩展。

    $(document).ready(function() {
      var myDates = ["10/18/2017", "10/20/2017"];
      myDates.forEach(function(dateString) {
        var date = new Date(dateString);
        var currMonth = date.getMonth() + 1;
        var currYear = date.getFullYear();
        var currDay = date.getDate();
    
        var $monthYear = $(`td[data-month='${currMonth}'][data-year='${currYear}']`);
        $monthYear.each(function(i, elem) {
          var calendarDay = $(elem).find("a").text();
          var currDate = `${currMonth}/${calendarDay}/${currYear}`;
          if (dateString === currDate) {
            $(elem).addClass("highlight");
          }
        });
      })
    })
    .highlight {
      background-color: yellow;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
      <tr>
        <td class=" ui-datepicker-week-end  ui-datepicker-current-day" data-handler="selectDay" data-event="click" data-month="10" data-year="2017">
          <a class="ui-state-default" href="#">18</a>
          <!-- needs class: ui-state-active-->
        </td>
    
        <td class=" ui-datepicker-week-end  ui-datepicker-current-day" data-handler="selectDay" data-event="click" data-month="10" data-year="2017">
          <a class="ui-state-default" href="#">19</a>
          <!-- needs class: ui-state-active-->
        </td>
    
        <td class=" ui-datepicker-week-end  ui-datepicker-current-day" data-handler="selectDay" data-event="click" data-month="10" data-year="2017">
          <a class="ui-state-default" href="#">20</a>
          <!-- needs class: ui-state-active-->
        </td>
      </tr>
    </table>

    【讨论】:

    • hmm... 看起来这个月的所有日子都会如此。您将如何定位特定的一天,然后为其添加课程? if(day=='18'){day.addClass('ui-state-active')}?
    • 我更新了我的答案更通用,它包括一个像你指定的数组并将类添加到日期
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多