我使用的是jQuery日期选择器,我想显示用户当前悬停的日期。我有下面的代码(你也可以试试JSFiddle的代码,http://jsfiddle.net/JGM85/):
$(function() {
$("#datepicker").datepicker();
$(".ui-state-default").live("mouseenter", function() {
$("h1").text($(this).text());
});
});
当前,当鼠标悬停在日期上时,日期的编号(即23)会输出到h1标签。我想更改它,以便它输出整个日期并将其存储在一个变量中。
如有任何帮助,我们将不胜感激。
这不是我做这件事的最佳方式,因为我在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());
});
});
http://jsfiddle.net/JGM85/1/
第二个版本,保存实际日期+事后提醒:
$(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);
});
});
http://jsfiddle.net/JGM85/2/
更新:我以前有.live作为事件处理程序,但.live不被弃用,.on()是要使用的方法。