[英]Change input type value if is mobile device
Guys I'm using jquery UI library for date picking and time picker addon. 伙计们,我正在使用jQuery UI库进行日期选择和时间选择器插件。 All work on PC but I want to change
input type
from text
to date
or time
. 都可以在PC上运行,但是我想将
input type
从text
更改为date
或time
。
When I test on mobile device I want mobile to use default datetime picker(scroller). 当我在移动设备上进行测试时,我希望移动设备使用默认的日期时间选择器(滚动条)。
By default I just use simple code for showing dialogs 默认情况下,我只使用简单的代码来显示对话框
$('.datepicker').datepicker();
$('.timepicker').timepicker();
So is it possible to check if it is a mobile device? 那么可以检查它是否是移动设备? If yes change type to date or time.
如果是,将类型更改为日期或时间。
I try to create function which will detect if a user connect with mobile device but it doesn't work 我尝试创建一个功能来检测用户是否与移动设备连接,但无法正常工作
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
var date = document.getElementById('datepicker');
date.setAttribute('type','date');
}
But this function work when I check if it is mobile 但是当我检查它是否可移动时此功能有效
function isMobile() { return ('ontouchstart' in document.documentElement); }
Here is full working code: 这是完整的工作代码:
<script>
(function() {
var date = $(".datepicker");
var time = $(".timepicker");
var isTouchDevice = 'ontouchstart' in document.documentElement;
if ( isTouchDevice ) {
date.attr("type", "date");
time.attr("type", "time");
} else {
date.attr("type", "text");
$('.datepicker').datepicker();
$('.timepicker').timepicker();
}
})();
</script>
You could check for all touch devices like this: 您可以检查所有触摸设备,如下所示:
var isTouchDevice = 'ontouchstart' in document.documentElement;
if ( isTouchDevice ) {
// do mobile handling
var date = $('.datepicker');
date.attr('type','date');
} else {
// do default handling
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.