Jquery 模糊后不显示日期选择器占位符

Jquery Datepicker placeholder not showing after blur

我有简单的 2 个文本字段。一个是普通文本,另一个是我用于日期选择器的。我想清除占位符 onfocus 并再次显示 onblur 这是我正在使用的以下代码:

$('input,textarea').focus(function(){
    $(this).data('placeholder',$(this).attr('placeholder'))
    console.log($(this).data('placeholder'));
    $(this).attr('placeholder','');
});
$('input,textarea').blur(function(){
    console.log("adding "+$(this).data('placeholder'));
    $(this).attr('placeholder',$(this).data('placeholder'));
});

对于普通文本,它工作正常,但对于 datepicker 文本字段占位符,模糊后不出现。是否有任何解决方案可以使 datepicker 文本字段的行为类似于简单文本字段

试试 DEMO

您可以为日期选择器设置一个例外,这样占位符就不会被清除:

$('input:not(#thedate),textarea:not(#thedate)').focus(function(){

由于日期选择器触发了两个焦点事件。第二次触发时,它摆脱了 属性 占位符。因此,只需在存储到 .data() 之前检查 属性。应该这样做

$('input,textarea').focus(function () {
    if ($(this).prop('placeholder')) {
        $(this).data('placeholder', $(this).prop('placeholder'))
        $(this).prop('placeholder', '');
    }
});
$('input,textarea').blur(function () {
    if (!$(this).prop('placeholder')) {
        $(this).prop('placeholder', $(this).data('placeholder'));
    }
});

这里有演示http://jsfiddle.net/dhirajbodicherla/ndnxznn3/25/