can i Highlight specific days - like all mondays tuesdays etc in datepicker js

荒凉一梦 提交于 2020-03-05 02:17:17

问题


I am trying to highlight days in datepicker. I am using the folowwing code to highlight them.BUt this highlights all the days of the datepicker.

  dates.data = ['0','1','2'] // days of week
 beforeShowDay: function (date) {   
     if($.inArray(date.getDay(), dates.data)) {
          // if it is return the following.
              return [true, 'ui-state-error', 'tooltip text'];
              } else {
                             // default
               return [true, '', ''];
              }
}

回答1:


 var highlight_dates = {};
    highlight_dates[ new Date( '03/10/2020' )] = new Date( '03/10/2020' );
    highlight_dates[ new Date( '02/21/2020' )] = new Date( '02/21/2020' );
    
    $('#datepicker').datepicker({
       beforeShowDay: function(date) {
          var highlight = highlight_dates[date];
            if( highlight ) {
                 return [true, "class-to-highlight", 'Special Event'];
            } else {
                 return [true, '', ''];
            }
       }
    });
 .class-to-highlight{
           background-color: #ff0;
       }
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Date: <input type="text" id="datepicker"></p>



回答2:


you can use bdeforeShowDay function with highlighter css.

var highlightDays = ["2020-2-21","2020-2-23","2020-2-11","2020-3-21",];
var date = new Date();
jQuery(document).ready(function() { 
    $( "#datepicker").datepicker({ 
        dateFormat: 'yy-mm-dd',
        beforeShowDay: function(date) {
            var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
            for (i = 0; i < highlightDays.length; i++) {
                if($.inArray(y + '-' + (m+1) + '-' + d,highlightDays) != -1) {
                    return [true, 'highlight', ''];
                }
            }
            return [true];
        }
    });
});
.highlight{
       background-color: red;
   }
<html lang="en">
  <head>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
  </head>
  <body>
    <p>Date: <input type="text" id="datepicker" /></p>
  </body>
</html>


来源:https://stackoverflow.com/questions/60300669/can-i-highlight-specific-days-like-all-mondays-tuesdays-etc-in-datepicker-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!