我在页面上运行此时间选择器(http://timepicker.co/)时遇到问题。
我脑子里有这个
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.js"></script>
我的脚本中有这个
var hDeb = document.createElement("input");
hDeb.className = "hDeb timepicker";
这在下面的其他脚本中
$(document).ready(function() {
$('.timepicker').timepicker({});
});
没有显示错误,但输入只是输入文本
最佳答案
如果您在添加<input class="timepicker">
之前尝试初始化时间选择器,那么您的$('.timepicker')
选择器将返回零个元素,并且将无法正常工作,如您在下一个代码段中所见:
// DOES NOT WORK
$(document).ready(function() {
$('.timepicker').timepicker();
$('body').append('<input type="text" class="hDeb timepicker" />');
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.js"></script>
您需要先添加输入,然后才选择输入并初始化时间选择器:
// WORK
$(document).ready(function() {
$('body').append('<input type="text" class="hDeb timepicker" />');
$('.timepicker').timepicker();
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.js"></script>
关于jquery - jQuery timepicker cdnjs.cloudflare不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45858535/