这是功能。我从谷歌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();
});
}
答案 0 :(得分:3)
正如其他人所提到的,为了实现与使用jQuery的Document Web API相同的功能,您将使用选择器。正如Arun P所说,你会做的
var clockDiv = $('#clock'); // select the div with an ID of clock
clockDiv.text( /* your text */ ); // set the text of the selected element to the passed argument
jQuery是一个库,它抽象Web API来帮助解决跨浏览器兼容性问题,并且通常可以更轻松地导航和操作DOM。
答案 1 :(得分:1)
to set text in a div
$('#clock')。text('some text');
答案 2 :(得分:0)
因为jQuery以不同的方式返回文档信息。如果我没记错的话,那就必须是:
$(document)[0].getElementById('clock');