简体   繁体   English

当我使用“==”时,为什么datepicker突出显示不起作用?

[英]Why does datepicker highlight not work when I use “==”?

   .Highlighted a{
   background-color : Green !important;
   background-image :none !important;
   color: White !important;
   font-weight:bold !important;
   font-size: 9pt;

}


  $(document).ready(function () {

                var date1 = new Date(2014, 5, 6);
                var date2 = new Date(2014, 5, 17);

                $('#datepicker').datepicker({

                   dateFormat: "mm/dd/yy",

                   beforeShowDay: function (date) {


                       if (date == date1 ) {

                            return [true, 'Highlighted', 'Available Date'];
                        }
                        return [false, '', ''];
                    }
                });
        });

This one doesn't work, because of date==date1 . 这个不起作用,因为date==date1 If I change it to date<=date1 , it works fine. 如果我将它更改为date<=date1 ,它可以正常工作。 I thought javascript is a weakly typed language and it compares the content, rather than reference. 我认为javascript是一种弱类型语言,它比较内容,而不是引用。 I don't wanna do something like (date.getDay==date1.getDay &&....) . 我不想做(date.getDay==date1.getDay &&....) Is there an easier way to compare the values? 是否有更简单的方法来比较这些值?

Demo Fiddle 演示小提琴

Use the + unary operator ( reference ) to convert the values to numerics for comparison. 使用+一元运算符( 引用 )将值转换为数值以进行比较。

The unary + operator converts its operand to Number type. 一元+运算符将其操作数转换为数字类型。


if (+date === +date1 ) {

      return [true, 'Highlighted', 'Available Date'];
}

OR 要么

if (!(date - date1)) {

      return [true, 'Highlighted', 'Available Date'];
}

正如本文所述,您希望使用以下内容来比较日期:

date.getTime() == date1.getTime()

其他方式:

if (date.valueOf() == date1.valueOf())

暂无
暂无

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

相关问题 为什么当我使用JQuery DatePicker时Google Code Prettifier停止工作(反之亦然) - Why does Google Code Prettifier stop working when I use JQuery DatePicker (or vice versa) 使用锚点时,为什么按键处理程序不起作用 - Why does my keypress handler not work when I use anchors 当我更改一个jQuery Datepicker的DOM位置时,它不起作用 - When I change the a JQuery datepicker's DOM place it does not work 当我使用“和”或“或”时,为什么我的替换在javascript中正常工作,但在我使用“&”或“/”或“+”时却没有 - Why does my replace work properly in javascript when I use “and” or “or” but not when I use “&” or “/” or “+” Bootstrap 3 scrollspy不起作用。 当我向下滚动然后突出显示时,它不会突出显示“关于” - Bootstrap 3 scrollspy does not work. It does not highlight “About” when i scroll down then it highlighted 为什么在运行For循环时不起作用? - Why does the For Loop not work when I run it? 当我在函数中使用if语句时,为什么我的javascript无法正常工作 - Why does my javascript not work when I use an if statement in my function 为什么我的代码使用underscore.js但不能使用Ramda.js? - Why does my code work with underscore.js but not when I use Ramda.js? 为什么当我尝试使用 React Native 钩子时,它无法按预期正常工作? - Why when I try to use react native hooks it does not work properly as intended to be? 当我通过浏览器打开html页面时,为什么倒计时应用程序可以正常运行,但是当我使用http服务器时却不能正常运行? - Why does my countdown app work fine when I open the html page through the browser, but not when I use an http server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM