我正在学习JavaScript而且我不懂如何将ID从html传递到JavaScript函数。
我的CSS页面在这里有:
#quizclock
(此处有属性)
在我的HTML页面上,我有一个javascript函数:
<script type="text/javascript">
var seconds = 0;
var clockId;
function runClock()
{
seconds + 1;
quizclock = seconds; //right here is my problem.
}
function startClock()
{
showQuiz();
runClock();
setInterval("runClock()", 1000);
}
function stopClock()
{
clearInterval(runClock);
gradeQuiz();
return = correctAns;
alert("You have " + correctAns + " correct out of 5 in " + quizclock + " seconds.");
}
</script>
所以我需要在函数中使用id quizclock。有什么提示吗?
答案 0 :(得分:1)
我注意到您的代码存在其他一些问题,我已经对修复程序进行了评论,并添加了其他一些提示。
var seconds = 0;
var clockId;
var correctAns;
// Lets get a reference to the quizclock element and save it in
// a variable named quizclock
var quizclock = document.getElementById('quizclock');
function runClock() {
// seconds + 1;
// This calculates seconds + 1 and then throws it away,
// you need to save it back in to the variable
// You could do that with:
// seconds = seconds + 1;
// But it would be even better with the shorthand:
seconds += 1;
// set the HTML inside of the quizclock element to new time
quizclock.innerHTML = seconds;
}
function startClock() {
showQuiz();
runClock();
// setInterval("runClock()", 1000);
// When using setInterval and setTimeout you generally just
// want to directly pass it the function by name. Passing it
// a string "runClock()" is in effect actually running
// eval("runClock()"), eval should be avoided unless you
// really need it.
// setInterval returns a number which identifies the interval,
// you need to save that number, you'll need it when you
// call clearInterval
clockId = setInterval(runClock, 1000);
}
function stopClock() {
// clearInterval takes the id that setInterval
// returned to clear the interval
clearInterval(clockId);
gradeQuiz();
// you had this alert statment after the return statement,
// it would have never run, return statements end the
// function and anything after them is ignored
alert("You have " + correctAns + " correct out of 5 in " +
quizclock + " seconds.");
//return = correctAns;
// the return statement doesn't need a =,
// return = correctAns says set a variable named return to the
// value of correctAns since return is a reserved word,
// that should generate an error
return correctAns;
}
如果这是一个正式的类,你可能只需要使用基本的DOM方法来获取元素(getElementById
等)。如果您只是自学,我会鼓励您学习DOM库。我建议jQuery,它是easy to learn,现在或多或少是事实上的标准。使用jQuery而不是document.getElementById('quizclock')
,您可以执行此操作:$('#quizclock')
。使用jQuery可以缩短代码,使不同浏览器之间的内容标准化,并有助于保护您免受这些浏览器中的错误的影响。
你现在只是一个初学者,在这样的小例子中你不必担心全局变量,但你应该知道使用太多变量通常是个坏主意。如果页面上的另一个函数也使用了名为seconds
的全局变量,该怎么办?它可能会改变seconds
并搞砸你的计时器。这有点进步,但避免这种情况的一种方法是将代码包装在self-invoking anonymous function中:
(function () {
var seconds = 0;
// inside here seconds is visible and can be used
}());
// outside seconds is not declared, it will return undefined.
不幸的是,内部的任何函数也不会在外部显示,因此通过onclick=
附加它们是行不通的,但您可以(应该)使用DOM附加它们:
var submitButton = document.getElementById('submitanswers'); // you'll have to give the button an id
submitButton.addEventListener('click', stopClock, false);
同样,使用jQuery会使这更容易:
$('#submitanswers').on('click', stopClock);
同样,如果您使用jQuery,它已经强制您将代码包装在一个函数中,该函数将使变量不在globalnamespace中:
$(document).ready(function () {
var seconds;
// again seconds is visible here
});
// but not here
答案 1 :(得分:0)
您可以选择一个元素:
var quizclock = document.getElementById('quizclock');
然后,您可以使用以下内容设置值:
quizclock.innerHTML = seconds;