有一个需要日期但没有日期选择器的文本框。我想用 Greasemonkey 添加一个。我寻找一个例子,但找不到。
这可能吗?有这样做的例子吗?它不需要很花哨。
我喜欢用jQuery UI 的 datepicker()因为我通常都会加载 jQuery UI,而且它相当知名/受支持。
不幸的是,日期选择器功能使用了一些非常糟糕的事件代码,这需要一些捏造才能在沙盒环境中工作。
不过,这并不太难。这是一个完整的 Greasemonkey 脚本:
// ==UserScript==
// @name _Datepicker fun
// @include http://YOUR_SERVER/YOUR_PATH/*
// @include http://jsbin.com/ukelij*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// @require https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js
// @resource jqUI_CSS http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css
// ==/UserScript==
//--- Date picker needs additional CSS
GM_addStyle (GM_getResourceText ("jqUI_CSS") );
//--- Add datepicker popups to select inputs:
$("#pickMe").datepicker ();
$("#pickMe").click ( function () {
setTimeout (cleanUpCrappyEventHandling, 100);
} );
/*--- Clean up ultra-crappy event handling ('til dev team eventually fixes).
This must be done to allow the picker to work in a sandboxed environment.
Alternately, you can modify the jQuery-UI source ala http://stackoverflow.com/q/2855403
*/
function cleanUpCrappyEventHandling () {
var nodesWithBadEvents = $(
"div.ui-datepicker td[onclick^='DP'], div.ui-datepicker a[onclick^='DP']"
);
nodesWithBadEvents.each ( function () {
var jThis = $(this);
var fubarFunc = jThis.attr ("onclick");
/*--- fubarFunc will typically be like:
DP_jQuery_1325718069430.datepicker._selectDay('#pickMe',0,2012, this);return false;
*/
fubarFunc = fubarFunc.replace (/return\s+\w+;/i, "");
jThis.removeAttr ("onclick");
jThis.click ( function () {
eval (fubarFunc);
cleanUpCrappyEventHandling ();
} );
} );
}
您可以加载它并对其进行测试此页面位于 jsBin.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)