我能模仿我想要的東西:http://jsfiddle.net/slicedtoad/hzg2ec30/9/
/////////////////////
// Makes a datepicker toggleable between inline and an emulated popup mode.
// Returns: function that toggles the datepicker.
// Params:
// - id of the container
// - options.datepicker and options.altfield (class names)
// ^defaults are .picker and .altfield
var makeToggleable = function (container, options){
options = typeof options !== 'undefined' ? options :
{datepicker:".picker",altfield:".altfield"};
var picker = options.datepicker;
var altfield = options.altfield;
var container = container; //in case selector refresh is needed
var $node = $(container).addClass("datepicker-toggleable");
var inline = false;
var open = false;
var bindOneOutsideMousedown = function(){
// binds one mousedown, rebinds if click was on the picker or altfield
$(document).one('mousedown',function closeOrBindAgain(e){
if (inline) {return;}
var $picker = $node.find(picker);
if (!$picker.is($(e.target)) // if the target of the click isn't the picker
&& !$(e.target).parents(picker).length // nor a descendant of the picker
&& !$node.find(altfield).is($(e.target))) { // nor the altfield
open = false;
$picker.addClass('invisible');
}else{
bindOneOutsideMousedown();
}
});
}
var onAltFieldMousedown = function() { //open picker on altfield click
if (open||inline) {return;}
open = true;
var $picker = $node.find(picker);
$picker.removeClass("invisible");
$picker.position({ //move it below the altfield
my: "left top",
at: "left bottom",
of: $node.find(altfield),
offset: "0 0",
collision: "none"
});
$picker.datepicker('option', 'onSelect',function (e) { //close on date select
$(this).trigger('change');
$picker.datepicker('option', 'onSelect', null); // turn off select listener
if(!inline){
$picker.addClass("invisible");
open = false;
}
});
bindOneOutsideMousedown();
};
var toggle = function() {
inline = !inline;
if (inline) { //inline mode
$node.addClass("inline").removeClass("popup");
$node.find(picker).removeClass("invisible");
$node.find(altfield).addClass("invisible");
} else { //popup mode
open = false;
$node.removeClass("inline").addClass("popup");
$node.find(picker).addClass("invisible");
$node.find(altfield).removeClass("invisible")
.on('mousedown', onAltFieldMousedown);
}
};
return toggle;
};
用法:
$("#StartDate .picker").datepicker({
altField: ".altfield"
});
var toggleStart = makeToggleable("#StartDate",{altfield:".altfield",datepicker:".picker"}); //makes #StartDate toggleable and returns the function to toggle it
$("#toggle").click('click', toggleStart);
$("#toggle").trigger('click');
基本上,它總是一個內嵌而裝在「彈出模式」時,彈出窗口。要完全像datepicker的彈出窗口模式,它需要一些動畫opacity
而不是設置爲display:none
。可能還有其他一些細節。
這裏,它正與在同一容器中的兩個datepickers和大量耦合:http://jsfiddle.net/slicedtoad/Lf71gjcc/3/
我不明白你正試圖在這裏實現什麼。也不是你爲什麼在JS中編寫你的html,而不是在頁面中。你能否詳細說明一下? – 2014-09-10 19:10:36
@Ramy小提琴是爲了確保解決方案不會暴露事件監聽器等。如果你想要一個簡化的:http://jsfiddle.net/slicedtoad/8xbyo5pj/1/ – slicedtoad 2014-09-10 19:58:55
我認爲最簡單的方法是摧毀它,並用'inline:true'重新初始化它..我不認爲你可以改變它已經初始化(至少據我所知)。因爲行爲非常不同。 – 2014-09-10 20:03:22