我对jQuery相对较新,并且很难从外部js文件中获取jQuery datepicker。
最初我将脚本创建为一个函数,但相信通过这样做我限制了范围,并且在函数外部无法访问它。我也尝试将其定义为函数(并命名函数),然后使用$(document).ready
调用它。无论如何,我无法让它发挥作用。
我的外部js脚本名为scripts.js,其内容如下:
$( "#from" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 2,
showOn: "both",
buttonImageOnly: true,
buttonImage: "images/calendar.gif",
dateFormat: "mm/dd/yy",
altField: "#forminp1",
altFormat: "yyddmm",
onClose: function( selectedDate ) {
$( "#to" ).datepicker( "option", "minDate", selectedDate );
}
});
$( "#to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 2,
showOn: "both",
buttonImageOnly: true,
buttonImage: "images/calendar.gif",
dateFormat: "mm/dd/yy",
altField: "#forminp2",
altFormat: "yyddmm",
onClose: function( selectedDate ) {
$( "#from" ).datepicker( "option", "maxDate", selectedDate );
},
});
HTML是:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Select a Date Range</title>
<link rel="stylesheet" href="css/jquery-ui-1.10.4.custom.css" type="text/css">
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui-1.10.4.custom.js"></script>
<script src="js/scripts.js"></script>
</head>
<body>
<label for="from">From</label>
<input type="text" id="from" name="from">
<label for="to">to</label>
<input type="text" id="to" name="to">
<p></p>
<input type="text" id="forminp1" size="30"> <input type="text" id="forminp2" size="30">
</body>
</html>
如何将jQuery代码保持在外部,但在页面加载时能够正常运行?