【问题标题】:I place a HTML class definition in style tag and then use it in beforeShowDay and it is undefined我在样式标签中放置了一个 HTML 类定义,然后在 beforeShowDay 中使用它,它是未定义的
【发布时间】:2016-01-19 15:13:56
【问题描述】:

我有以下几点:

$("<style>")
    .prop("type", "text/css")
    .html("\
#markDates {\
    color: #FF0000;\
}")
    .appendTo("head");

然后我调用这个类:

 $('#multidatepicker').multiDatesPicker({
        //onClose: function (value, date) {
        //    //debugger;
        //    date.dpDiv.find('.ui-state-default').css('background-color', 'rgb(153, 204, 153)')
        //}, 
        numberOfMonths: [3, 4],
        //addDisabledDates: HolidayList,
        addDates: getHolidays(),
        beforeShowDay: function (date) {
        //    console.log('dateIn?', $.inArray(date, HolidayList));
            if ($.inArray(date, HolidayList) !== -1) {
                $("td").removeClass("ui-state-highlight");
                return [true, "markDates" , "This is a holiiday"];
            }
            else {
                return [true];
            }
        }
    });

当我运行它然后检查突出显示的日期元素时,会发现以下内容:

<td class=" ui-state-highlight undefined " data-handler="selectDay" data-event="click" data-month="0" data-year="2016"><a class="ui-state-default" href="#">1</a></td>

为什么 markDates 未定义?我尝试过:

removeClass("ui-state-highlight").addClass("markDates") 

结果相同。

【问题讨论】:

  • #markDates 在第一个脚本中是一个id。而removeClass("ui-state-highlight").addClass("markDates") 正在添加类markDates
  • 您已将最后一个右括号 //} 注释掉为 beforeShowDay
  • @Filipe 感谢您的更改。我进行了适当的更改,但仍然在元素中显示为未定义。
  • “这是一个假期”也不会显示为工具提示。
  • 它可能碰到了if ... else 块的else 部分,并试图将类设置为beforeShowDay 返回值的第二个索引。那是未定义的。我敢打赌,如果您将return [true]; 更改为return [true, 'noMarkDates'];,它将不再是未定义的,但它的类将是 noMarkDates。

标签: javascript jquery-ui-datepicker


【解决方案1】:

它是未定义的,因为它在beforeShowDay 中遇到了您的else 语句。此方法必须采用返回值的第二个索引并将其用作日项的类。将return [true]; 更改为return [true, 'noMarkDates']; 可以纠正未定义的问题,但并不能真正解决问题,因为我们确实想突出显示特定的日子。我认为这是HolidayList 的问题。 beforeShowDaydate 变量使用日期对象。您正在尝试查看相同的日期对象是否在日期数组中。 (至少在我看来)由于格式不一致或日期对象中的信息过多(日期对象包括小时、分钟、秒和毫秒,而您想要的只是日、月、年)。您可以通过先格式化日期来纠正此问题。

beforeShowDay: function (date) {
  // first get a consistent format for your date
  var d = jQuery.datepicker.formatDate('mm-dd-yy', date);
  // then compare it against an array of similarly formatted date strings
  if ($.inArray(d, ["01-23-2016","03-17-2016","03-16-2016"]) !== -1) {
    return [true, "markDates" , "This is a holiiday"];
  }
  else {
    return [true, 'noMarkDates'];
  }
}

结果是突出显示正确的日期:https://jsfiddle.net/jmarikle/1gr6237q/1/

另外,请注意我使用了.markDates { background: red; } 样式。您的使用 ID 而不是类(.# 的区别)。请改用类。

从这里获得formatDate 位:https://stackoverflow.com/a/28852510/854246

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多