样式日期选择器:突出显示特定日期

时间:2016-07-22 12:55:08

标签: jquery css wordpress datepicker

我知道之前已经问过这个问题'解决方案'我发现到目前为止似乎没有工作。

我在wordpress网站上使用Contact Form 7和datepicker插件。联系表格和日历工作正常但我希望能够通过更改这些日期的背景颜色来突出显示特定日期。

这是我在头文件中包含的代码:

<script type="text/javascript">
  $(document).ready(function() {
    var SelectedDates = {};
    SelectedDates[new Date('07/26/2016')] = new Date('07/26/2016');
    $('#datepicker123').datepicker({
      beforeShowDay: function(date) {
        var Highlight = SelectedDates[date];
        if (Highlight) {
          return [true, "Highlighted", Highlight];
        }
        else {
          return [true, '', ''];
        }
      }
    });
  });
</script>

在我的style.css表中,我已经包含以下代码来为这些日期添加样式:

.Highlighted a{
   background-color : #1AAFFF !important;
}

但是,当我点击日历但出现标准样式时,此示例中的日期(7/26/2016)不会突出显示。哪里是我的错?

非常感谢你的帮助!

编辑:HTML代码似乎超长,所以这里是指向该网站的链接:KYTE

编辑2:所以我已将以下代码添加到我的functions.php文件中:

function wpse_enqueue_datepicker() {
  // Load the datepicker script (pre-registered in WordPress).
  wp_enqueue_script( 'jquery-ui-datepicker' );

  // You need styling for the datepicker.
  // For simplicity I've linked to Google's hosted jQuery UI CSS.
  wp_register_style( 'jquery-ui', 'http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css' );
  wp_enqueue_style( 'jquery-ui' );  
}

add_action( 'wp_enqueue_scripts', 'wpse_enqueue_datepicker' );

仍然无法正常工作。

3 个答案:

答案 0 :(得分:0)

我认为您没有将突出显示的类设置为选定日期。 试试

$('td').on('click', function(){
    $('td.Highlighted').removeClass('Highlighted');
    $(this).addClass('Highlighted');
});
这对你有所帮助 并改变你的CSS:

tr.Highlighted {
     background-color:red;
}

我测试了它

答案 1 :(得分:0)

您正试图在其库加载之前调用datepiker方法。

尝试将此代码放入functions.php文件

<?php
  add_action( 'wp_footer', 'my_custom_js');
  function my_custom_js(){

   //Your js code goes here

  }
?>

答案 2 :(得分:0)

在我将脚本放入页脚之前,我一直困惑不已。然后它奏效了。我有单独的js和css,这有助于只在需要时将它们排队。

function se_3852702_init() {
  // Place your own script in footer (see the last TRUE)
  wp_register_script( 'se_3852702',
    plugins_url( 'se_3852702' ) . DIRECTORY_SEPARATOR . 'se_3852702.js',
    [ 'jquery-ui-datepicker' ],
    FALSE,
    TRUE // <======== Place in the footer
  );

  wp_register_style( 'se_3852702', plugins_url( 'se_3852702' ) . DIRECTORY_SEPARATOR . 'se_3852702.css' );
}
add_action( 'init', 'se_3852702_init' );

function se_38527021_enqueue_scripts() {
  wp_enqueue_script( 'se_3852702' );
  wp_enqueue_style( 'se_3852702' );

  wp_enqueue_style( 'jquery-ui', 'http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css' );
}
// This is overkill as it always enqueue the scripts.
add_action( 'wp_enqueue_scripts', 'se_38527021_enqueue_scripts' );

与类似的js脚本一起

jQuery(document).ready(
  (function ($) {
    function dayAvailable(date) {
      console.log(date);
      let day = date.getDate();
      return [day % 2 == 0, 'my-class', 'Label when hover'];
  }

  $('#date-picker').datepicker({
    beforeShowDay: dayAvailable,
  })
})(jQuery)
);

导致

Wordpress datepicker