gpt4 book ai didi

javascript - document.getElementById() 返回 null,尽管在 $(document).ready 中被调用

转载 作者:行者123 更新时间:2023-11-30 09:28:45 25 4
gpt4 key购买 nike

<分区>

我正在为网页上的秒表开发圈数功能。这个想法是有一个 div,每当您单击以记录一圈时,当前时间都会附加到该 div。但是,当我尝试引用 lapLog div 时,我从 getElementById 函数返回 null。我知道页面必须完全加载才能发现所有元素,这就是为什么我将按钮调用的函数放在 $(document).ready block 中,但尽管如此它仍然返回 null。

相关 JavaScript (timer.js):

$(document).ready(function(){
$('#lapClkBtn').click(function(){
// Only allow laps to be recorded if the timer is running
if (ClkTimer.isAlive() == true) {
var newEntry = ClkTimer.recordLap();
lapLog = document.getElementById('lapLog');
lapLog.innerHTML.append(newEntry);
lapLog.animate({
scrollTop: lapLog.prop('scrollHeight')
}, 100);
}
});
})

function clkTimer(text, updateinterval) {
this.recordLap = function() {
this.lapNum = this.lapNum + 1;
return String(this.lapNum) + "| " + this.formattedTime() + "<br>";
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="lapClkBtn">Lap</button>
<div class="lapLog"></div>

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com