javascript中的日期选择器

时间:2012-04-30 07:32:03

标签: javascript date datepicker mootools

我的网页上没有日期选择器但是小提琴它即将到来,我缺少什么?

http://jsfiddle.net/cBwEK/

它在小提琴中变好了,但我的网页上什么都没有。一世  写了以下脚本

整个源代码在这里:http://www.monkeyphysics.com/mootools/script/2/datepicker

<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>     
    <script type="text/javascript" src="datepicker.js"></script>      
    <script type="text/javascript" src="mootools-yui-compressed.js"></script>
    <script type="text/css" src="datepicker.css"></script>        
    <script type="text/javascript">           
        new DatePicker('.picker', {
            pickerClass: 'datepicker ',
            allowEmpty: true
        });           
    </script>        
 </head>
 <body>
     <label>Datepicker starting at and allowing no value:</label>
     <input name='date_allow_empty' type='text' value='' class='date picker' />
 </body>
</html>

2 个答案:

答案 0 :(得分:4)

在 DOM中存在相关元素之后,您需要运行JavaScript代码。只需将脚本移动到页面末尾即可。此外,如果日期选择器脚本依赖于MooTools,则需要在 MooTools include之后添加datepicker include 。 E.g:

<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>     
    <!-- FIRST CHANGE HERE, swapping the order of the script elements -->
    <script type="text/javascript" src="mootools-yui-compressed.js"></script>
    <script type="text/javascript" src="datepicker.js"></script>      
    <link type="text/css" rel="stylesheet" href="datepicker.css">
 </head>
  <body>
   <label>Datepicker starting at and allowing no value:</label>
   <input name='date_allow_empty' type='text' value='' class='date picker' />
   <!-- SECOND CHANGE HERE, moving your script to *after* the elements it operates on -->
   <script type="text/javascript">           
            new DatePicker('.picker', {
     pickerClass: 'datepicker ',
     allowEmpty: true
     });           
   </script>        
  </body>
</html>

Live copy | source

我刚刚移动了您的脚本,但您可能会将所有脚本移到那里as the YUI people suggest

它在jsFiddle中工作的原因是你有一个小提琴集来加载来自onload事件的JavaScript代码,这个事件在这个过程的后期发生;到那时DOM元素就在那里。

答案 1 :(得分:2)

尝试在页面加载后加载DatePicker初始化。

A good programmer always use View Source

如果你看过Fiddle,你会看到:

<script type='text/javascript'>
    window.addEvent('load', function() {
        new DatePicker('.picker', {
             pickerClass: 'datepicker ',
            allowEmpty: true
        });
    });
</script>

那是因为没有加载HTML元素而你的脚本看不到它们。