简体   繁体   English

使用beforeShow()在datepicker中添加类

[英]Add class in datepicker using beforeShow()

I'm trying to add a class on the selected date element using this function: 我正在尝试使用此功能在所选日期元素上添加一个类:

beforeShow:function(){ $(".ui-datepicker-current-day").addClass('testX') }

I have two issues. 我有两个问题。 First, no such class has been added. 首先,没有添加此类。 Second, the log shows a strange behavior. 其次,日志显示了奇怪的行为。 The first two times that I open the datePicker, hasClass() returns false but on the third time shows true (while the testX class still not shown in the html). 我打开datePicker的前两次, hasClass()返回false,但是第三次​​显​​示true(而testX类仍未显示在html中)。

  $(function() { $( "#datepicker" ).datepicker({ beforeShow:function(){ console.log('================================================'); console.log($( ".ui-datepicker-current-day" ).hasClass('testX')); $( ".ui-datepicker-current-day" ).addClass('testX'); console.log($( ".ui-datepicker-current-day" ).hasClass('testX')); } }); }); 
 .container { width: 600px; padding: 20px; margin: auto; background: #ddd; } .testX{ background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script> <link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <link href="//code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <div class="container"> <p>This is a datepicker example using jquery, jquery ui, and jquery css</p> <form> Date: <input id="datepicker"> </form> </div> 

$("#datepicker").datepicker({
            afterShow: function () {
                console.log('================================================');
                console.log($(".ui-datepicker-current-day").hasClass('testX'));
                $(".ui-datepicker-current-day").addClass('testX');
                console.log($(".ui-datepicker-current-day").hasClass('testX'));
            }
        });

The above code has been updated you can use onfous event on #datetimepicker. 上面的代码已更新,您可以在#datetimepicker上使用onfous事件。 the below given code might be helpful for you. 以下给出的代码可能对您有所帮助。 it works for me. 这个对我有用。

  $(function () {

       $("#datepicker").focus(function(){
              console.log('================================================');
                console.log($(".ui-datepicker-current-day").hasClass('testX'));
                $(".ui-datepicker-current-day").addClass('testX');
                console.log($(".ui-datepicker-current-day").hasClass('testX'));

         });

    });

Try this code 试试这个代码

 var selectedDay = new Date().getDate(); $(function() { $( "#datepicker" ).datepicker({ onSelect: function(dateText, inst) { selectedDay = inst.selectedDay; }, beforeShowDay: function (date) { if (date.getDate() == selectedDay && !$('#datepicker').val()=="") { return [true, "testX", ""]; } else { return [true, ""] } } }); }); 
 .container { width: 600px; padding: 20px; margin: auto; background: #ddd; } .testX{ background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script> <link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <link href="//code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <div class="container"> <p>This is a datepicker example using jquery, jquery ui, and jquery css</p> <form> Date: <input id="datepicker"> </form> </div> 

Hope this helps 希望这可以帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM