這是功能。我包含來自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);' –