我正在使用JQueryMobile's experiment Datepicker一段时间,它在旧的框架中工作。昨天我将框架更改为最新版本,发现当我点击外面时它会破坏Datepicker的布局。
有没有人遇到同样的问题?你怎么过来的?这意味着最新的框架将不支持datepicker ??
我的代码如下
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="jquery.mobile-1.1.0.min.css" />
<link rel="stylesheet" href="jquery.ui.datepicker.mobile.css" />
<script src="jquery-1.7.2.min.js"></script>
<script>
//reset type=date inputs to text
$( document ).bind( "mobileinit", function(){
$.mobile.page.prototype.options.degradeInputs.date = true;
});
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
<script src="jquery.mobile-1.1.0.min.js"></script>
<script src="jQuery.ui.datepicker.js"></script>
<script src="jquery.ui.datepicker.mobile.js"></script>
</script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Page Title</h1>
</div><!-- /header -->
<div data-role="content">
<label for="date">Date Input:</label>
<input type="date" name="date" id="date" value="" />
<div id='datepicker'></div>
</div><!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
</body>
</html>
答案 0 :(得分:3)
我遇到了同样的问题,我刚刚更改了日期选择器API。 使用这个它会给你想要的每一件事,比如像iOS,Android,jQuery Mobile,模式,模态等UI,与jQuery代码一起设置。
使用此功能后,问题永远不会出现。
答案 1 :(得分:2)
在 jquery.ui.datepicker.mobile.js 中查找此代码 就在updateDatepicker();
之下$( ".ui-datepicker-calendar .ui-btn", dp ).each(function(){
var el = $(this);
// remove extra button markup - necessary for date value to be interpreted correctly
el.html( el.find(".ui-btn-text").text() );
});
替换
el.html( el.find(".ui-btn-text").text() );
带
el.text( el.text() );
它对我有用:)
答案 2 :(得分:1)
答案 3 :(得分:1)
我用它:
使用更新版本的datepicker会打破月/年更改的布局。
来自here,我得到了我需要的文件。我在不同的stackoverflow问题上使用了几个答案,进行了以下更改:
jquery.ui.datepicker.mobile.js新代码:
(function ($, undefined) {
//cache previous datepicker ui method
var prevDp = $.fn.datepicker;
//rewrite datepicker
$.fn.datepicker = function (options) {
var dp = this;
//call cached datepicker plugin
var retValue = prevDp.apply(this, arguments);
//extend with some dom manipulation to update the markup for jQM
//call immediately
function updateDatepicker() {
$(".ui-datepicker-header", dp).addClass("ui-body-c ui-corner-top").removeClass("ui-corner-all");
$(".ui-datepicker-prev, .ui-datepicker-next", dp).attr("href", "#");
$(".ui-datepicker-prev", dp).buttonMarkup({ iconpos: "notext", icon: "arrow-l", shadow: true, corners: true });
$(".ui-datepicker-next", dp).buttonMarkup({ iconpos: "notext", icon: "arrow-r", shadow: true, corners: true });
$(".ui-datepicker-calendar th", dp).addClass("ui-bar-c");
$(".ui-datepicker-calendar td", dp).addClass("ui-body-c");
$(".ui-datepicker-calendar a", dp).buttonMarkup({ corners: false, shadow: false });
$(".ui-datepicker-calendar a.ui-state-active", dp).addClass("ui-btn-active"); // selected date
$(".ui-datepicker-calendar a.ui-state-highlight", dp).addClass("ui-btn-up-e"); // today"s date
$(".ui-datepicker-calendar .ui-btn", dp).each(function () {
var el = $(this);
// remove extra button markup - necessary for date value to be interpreted correctly
// only do this if needed, sometimes clicks are received that don't require update
// e.g. clicking in the datepicker region but not on a button.
// e.g. clicking on a disabled date (from next month)
var uiBtnText = el.find(".ui-btn-text");
if (uiBtnText.length)
el.html(uiBtnText.text());
});
};
//update after each operation
updateDatepicker();
$( dp ).on( "click change", function( event, ui)
{
$target=$(event.target);
if(event.type=="click" && ($target.hasClass("ui-datepicker-month") || $target.hasClass("ui-datepicker-year")))
event.preventDefault();
else
updateDatepicker( event);
});
//return jqm obj
if (retValue) {
if (!retValue.jquery) return retValue;
}
return this;
};
})(jQuery);
我使用on()而不是click事件,我在单击月/年选择菜单时阻止默认。
我用这种方式:
$form
.trigger( "create" )
.find( "input[type='date'], input:jqmData(type='date')")
.each(function()
{
$(this)
.after( $( "<div />" ).datepicker(
{
altField : "#" + $(this).attr( "id" ),
altFormat : "dd/mm/yy",
showOtherMonths : true,
selectOtherMonths : true,
showWeek : true,
changeYear : true,
changeMonth : true,
beforeShowDay : beforeShowDay
}));
});
with beforeShowDay是一个返回数组的函数(参见jqueryui datepicker手册)。
这对我有用,我现在只需要在日期输入得到关注时添加事件,只显示日历。
答案 4 :(得分:0)
其他地方的答案副本:
我致力于将jquery ui datepicker更新为最新版本的jquery,jqueryui和jquery mobile,以便jq1.9.1 jqui 1.10.2和jqm 1.3.0。我喜欢像以前的回答一样离开,所以你可以看到它是如何演变的。
changeMonth和changeYear下拉列表需要特别注意工作(不经常使用)
这是我如何更新jqmobile的实验性jqueryui datepicker:
您可以链接到datepicker脚本而不是整个jqueryui包。
readonly prop阻止键盘出现在ios上
这只是对日期选择器进行调整以使其在jqm上运行,目标是能够覆盖datepicker小部件的_generatehtml函数,并能够作为输入使用jquery移动主题。所以你不需要那一堆addClass并避免不必要的DOM操作
我只测试了ios 6(iphone,ipad)和桌面(chrome,firefox,safari),让我们了解其他测试。
希望它能够根据需要提供帮助:)
这里是以html,js和css分隔的所有代码:
<强> HTML 强>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Jqueryui 1.10.2 datepicker Integration in jquery mobile 1.3.0 and jquery 1.9.1 by aureltime</title>
<link rel="stylesheet" href="//ajax.aspnetcdn.com/ajax/jquery.mobile/1.3.0/jquery.mobile-1.3.0.min.css">
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>jQuery UI's Datepicker Styled for mobile adapted by Aureltime</h1>
</div>
<div data-role="content">
<form action="#" method="get" id="form">
<div data-role="fieldcontain">
<label for="date">Date:</label>
<input type="date" name="date" id="date" value="" />
</div>
</form>
</div>
</div>
</body>
</html>
<强> JS 强>
//reset type=date inputs to text
$.mobile.page.prototype.options.degradeInputs.date = true;
$("#form").trigger("create");
$( document )
.on( "pageinit", function(){
$("#date")
.prop("readonly", "true")
.on("click", function(){
$input=$(this);
$next=$input.next();
if($next.hasClass("hasDatepicker"))
$next.hide();
$input
.hide()
.after( $( "<div />", { id : "datepicker_"+$input.attr("id")}).datepicker(
{
altField : "#" + $input.attr( "id" ),
altFormat : "dd/mm/yy",
defaultDate : $input.val(),
showOtherMonths : true,
selectOtherMonths : true,
//showWeek : true,
changeYear : true,
changeMonth : true,
//showButtonPanel : true,
//beforeShowDay : beforeShowDay,
onSelect : function( dateText, inst)
{ $("#datepicker_"+$input.attr("id")).hide();
$input.show();
}
}));
});
});
(function($, undefined ) {
//cache previous datepicker ui method
var prevDp = $.fn.datepicker;
//rewrite datepicker
$.fn.datepicker = function( options ){
var dp = this;
//call cached datepicker plugin
prevDp.call( this, options );
//extend with some dom manipulation to update the markup for jQM
//call immediately
function updateDatepicker(event){
$( ".ui-datepicker-header", dp ).addClass("ui-body-c ui-corner-top").removeClass("ui-corner-all");
$( ".ui-datepicker-prev, .ui-datepicker-next", dp ).attr("href", "#");
$( ".ui-datepicker-prev", dp ).buttonMarkup({iconpos: "notext", icon: "arrow-l", shadow: true, corners: true});
$( ".ui-datepicker-next", dp ).buttonMarkup({iconpos: "notext", icon: "arrow-r", shadow: true, corners: true});
$( ".ui-datepicker-calendar th", dp ).addClass("ui-bar-c");
$( ".ui-datepicker-calendar td", dp ).addClass("ui-body-c");
$( ".ui-datepicker-calendar a", dp ).buttonMarkup({corners: false, shadow: false});
$( ".ui-datepicker-calendar a.ui-state-active", dp ).addClass("ui-btn-active"); // selected date
$( ".ui-datepicker-calendar a.ui-state-highlight", dp ).addClass("ui-btn-up-e"); // today"s date
if(typeof event != "undefined")
{
var classe= $(event.target).attr("class");
//alert(classe);
}
$( ".ui-datepicker-calendar .ui-btn", dp ).each(function(){
var el = $(this);
var buttonText = el.find( ".ui-btn-text" );
// remove extra button markup - necessary for date value to be interpreted correctly
if(buttonText.length)
el.html( el.find( ".ui-btn-text" ).text() );
});
// }
$( dp )
.off()
.on( "click", updateDatepicker)
.find("select")
.on( "change", function(event){updateDatepicker(event);});
}
//update now
updateDatepicker();
//return jqm obj
return this;
};
})( jQuery );
CSS
div.hasDatepicker{ display: block; padding: 0; overflow: visible; margin: 8px 0; }
.ui-datepicker { overflow: visible; margin: 0; max-width: 500px; }
.ui-datepicker .ui-datepicker-header { position:relative; padding:.4em 0; border-bottom: 0; font-weight: bold; }
.ui-datepicker .ui-datepicker-prev, .ui-datepicker .ui-datepicker-next { padding: 1px 0 1px 2px; position:absolute; top: .5em; margin-top: 0; text-indent: -9999px; }
.ui-datepicker .ui-datepicker-prev { left:6px; }
.ui-datepicker .ui-datepicker-next { right:6px; }
.ui-datepicker .ui-datepicker-title { margin: 0 2.3em; line-height: 1.8em; text-align: center; }
.ui-datepicker .ui-datepicker-title select { font-size:1em; margin:1px 0; }
.ui-datepicker select.ui-datepicker-month-year {width: 100%;}
.ui-datepicker select.ui-datepicker-month,
.ui-datepicker select.ui-datepicker-year { width: 49%;}
.ui-datepicker table {width: 100%; border-collapse: collapse; margin:0; }
.ui-datepicker td { border-width: 1px; padding: 0; text-align: center; }
.ui-datepicker td span, .ui-datepicker td a { display: block; padding: .2em 0; font-weight: bold; margin: 0; border-width: 0; text-align: center; text-decoration: none; }
.ui-datepicker-calendar th { padding-top: .3em; padding-bottom: .3em; }
.ui-datepicker-calendar th span, .ui-datepicker-calendar span.ui-state-default { opacity: .3; }
.ui-datepicker-calendar td a { padding-top: .5em; padding-bottom: .5em; }
答案 5 :(得分:0)
我已经制作了一个自定义日期时间选择器,有效用于Android和iphone翻转(两个翻转中的日期和时间)希望它会有所帮助