Javascript 类型错误:$.datepicker 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11918991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: $.datepicker is undefined
提问by rajeev
my javascript has code, for one of the pages on my website:
我的 javascript 有代码,用于我网站上的一个页面:
$('#nmdt1').datetimepicker({
dateFormat: $.datepicker.ATOM,
minDate: nmsdt,
...
...
this runs fine, when the page on which id="nmdt1" is loaded. And I load the related datetimepicker js library (module) only on when i load that page. so far so good.
当加载 id="nmdt1" 的页面时,这运行良好。我仅在加载该页面时加载相关的 datetimepicker js 库(模块)。到目前为止,一切都很好。
but when i load any other pages on my websit i get this error: from the line number where dateformat is defined.
但是当我在我的网站上加载任何其他页面时,我收到此错误:从定义日期格式的行号。
EDIT: here is the correct error for firebug log:
编辑:这是萤火虫日志的正确错误:
TypeError: $.datepicker is undefined
http://myswbsite/jscript/myjsscript.js
Line 569
line 569 is:
第 569 行是:
dateFormat: $.datepicker.ATOM,
and yes, this error only comes on page where I am not loading the related js code (jquery-ui-timepicker-addon.js). The reason I am not loading this js on every page is, i need it on only one page.
是的,这个错误只出现在我没有加载相关 js 代码(jquery-ui-timepicker-addon.js)的页面上。我没有在每个页面上加载这个 js 的原因是,我只需要在一个页面上使用它。
MORE DETAILS:
更多细节:
in HTML header following lib loads (in seq)
在 lib 加载后的 HTML 标头中(按顺序)
<head>
<script src="/jscript/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="/jscript/myjsscript.js" type="text/javascript"></script>
...
...
<script type="text/javascript">
jQuery(document).ready(function(){
var mid = "[% mid %]";
alert('mid='+mid);
$(".bvmainmenu #"+mid).css({"background":"url(/images/current-bg.gif) top left repeat-x", "color":"#ffffff"});
});
</script>
</head>
this last javascript code you see above (bottom of header) does not run each time when the jquery-ui-timepicker-addon.js lib is not loaded (and you see that err in firebug - i can live with error, but why this last code is not running, i am not sure). I am not able to understand why this routine wont run just because i did not load one 'add-on' library
每次未加载 jquery-ui-timepicker-addon.js 库时,您在上面看到的最后一个 javascript 代码(标题底部)不会运行(并且您在 firebug 中看到该错误 - 我可以忍受错误,但为什么会这样最后一个代码没有运行,我不确定)。我无法理解为什么这个例程不会运行只是因为我没有加载一个“附加”库
the page which runs everything correctly loads following js scripts in BODY
在 BODY 中正确加载以下 js 脚本的页面
<script src="/jscript/jquery-ui-1.8.21.custom.min.js" type="text/javascript"></script>
<script src="/jscript/jquery-ui-timepicker-addon.js" type="text/javascript"></script>
on this page the last javascript code you see in header also loads and displays the alert!
在此页面上,您在标题中看到的最后一个 javascript 代码也会加载并显示警报!
I am having tough time to figure this.
我很难弄清楚这一点。
采纳答案by nnnnnn
You seem to be saying that this code:
你似乎在说这段代码:
$('#nmdt1').datetimepicker({
dateFormat: $.datepicker.ATOM,
minDate: nmsdt,
...
...is within your common myjsscript.js
script that is loaded on every page. If so, that means it runs on every page and thus you get an error on pages that don't also include the extra plugin scripts.
...在您myjsscript.js
在每个页面上加载的通用脚本中。如果是这样,这意味着它在每个页面上运行,因此在不包含额外插件脚本的页面上会出现错误。
The code I've quoted above does notmean "If an element with that id exists call the datetimepicker()
method", it means "Create a jQuery object that may or may not have any elements in it and then call the datetimepicker()
method, passing an object with a property set to $.datepicker.ATOM
." That is, even if there is no nmdtd1 element on the page it will still call datetimepicker
and still reference $.datepicker.ATOM
.
我上面引用的代码并不意味着“如果存在具有该 id 的元素调用该datetimepicker()
方法”,它的意思是“创建一个 jQuery 对象,其中可能包含或不包含任何元素,然后调用该datetimepicker()
方法,传递一个对象属性设置为$.datepicker.ATOM
。” 也就是说,即使页面上没有 nmdtd1 元素,它仍然会调用datetimepicker
并引用$.datepicker.ATOM
.
There are at least three ways you can fix this:
至少有三种方法可以解决这个问题:
Move that code out of the common
myjsscript.js
and just put it on the one page that needs it.Go ahead and include the plugin JS files on all the pages on your site - they'll be be cached by the browser, so it's not really a performance hit assuming your users visit several of your pages anyway.
Move that code within a conditional so the
.datetimerpicker()
part is not executed unless needed.
将该代码移出公共区域
myjsscript.js
,并将其放在需要它的页面上。继续并在您网站上的所有页面上包含插件 JS 文件 - 它们将被浏览器缓存,因此假设您的用户无论如何都访问您的多个页面,这并不是真正的性能损失。
在条件中移动该代码,因此
.datetimerpicker()
除非需要,否则不会执行该部分。
For option 3:
对于选项 3:
var $nmdt1 = $('#nmdt1');
if ($nmdt1.length > 0) {
$nmdt1.datetimepicker({
dateFormat: $.datepicker.ATOM,
minDate: nmsdt,
...
});
}
回答by Arun P Johny
回答by sree
Make sure that $ is your jquery shortcut identifier. Check the usage of
确保 $ 是您的 jquery 快捷方式标识符。检查使用情况
var $J = jQuery.noConflict();
In that case try to use $J.datepicker
在这种情况下尝试使用 $J.datepicker