这是功能。我包含来自Google CDN的Jquery库,它位于此脚本之前。 {
$(document).ready(function() {
function displayTime() {
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
// This gets a "handle" to the clock div in our HTML
//This does not work???
var clockDiv = $(document).getElementById('clock');
//This works though
var clockDiv = document.getElementById('clock');
// Then we set the text inside the clock div
// to the hours, minutes, and seconds of the current time
clockDiv.innerText = hours + ":" + minutes + ":" + seconds;
}
// This runs the displayTime function the first time
displayTime();
});
}
因为jQuery没有getElementById方法...... ?????????? –
$(document).'返回一个jQuery对象,其中'getElementById'是'document'对象的一个方法 –
在jQuery中,您可以使用id选择器来获取对象'var clockDiv = $('#clock ');'then'clockDiv.text(hours +“:”+ minutes +“:”+ seconds);' –