我正在使用jQuery datepicker,我想显示用户当前悬停的日期。我有下面的代码(您也可以使用JSFiddle尝试代码,http://jsfiddle.net/JGM85/):
$(function() {
$("#datepicker").datepicker();
$(".ui-state-default").live("mouseenter", function() {
$("h1").text($(this).text());
});
});
目前,当悬停在某个日期上时,日期(即23)的数量将输出到h1标签。我想改变它,以便输出整个日期并将其存储在变量中。
任何有关此的帮助将不胜感激。
答案 0 :(得分:7)
这不是我做的最好的方式,因为我在jQuery UI Datepicker文档中找不到获取实际日期的任何内容,但是在这里你去了:
$(function() {
$("#datepicker").datepicker();
$(".ui-state-default").on("mouseenter", function() {
$("h1").text($(this).text()+"."+$(".ui-datepicker-month",$(this).parents()).text()+"."+$(".ui-datepicker-year",$(this).parents()).text());
});
});
第二个版本保存实际日期+之后提醒它:
$(function() {
$("#datepicker").datepicker();
$(".ui-state-default").on("mouseenter", function() {
$("h1").text($(this).text()+"."+$(".ui-datepicker-month",$(this).parents()).text()+"."+$(".ui-datepicker-year",$(this).parents()).text());
var actualDate=$('h1').text();
alert(actualDate);
});
});
更新:我之前有.live作为事件处理程序但是.live没有被弃用,而.on()是要去的方法。