gpt4 book ai didi

Javascript 在正文中有效,但在标题中无效

转载 作者:行者123 更新时间:2023-11-30 12:09:21 24 4
gpt4 key购买 nike

<分区>

据我所知,javascript 的首选格式位于标题末尾的单独文件中。我编写了一个简单的屏幕,它按时间间隔递增 indx,如果按下 Previous 按钮则反转。如果我在正文末尾包含脚本,它工作正常,但如果我在标题末尾包含按钮则无效。我在这里错过了什么吗?有人可以解释一下吗?

var buttons = document.getElementsByTagName("button");
for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick = handleButtonPress;
}

var del = 5000; /* time delay in milli seconds */
var indx = 0;
var direct = 1;
var intervalID = window.setInterval(proceed, del);

function proceed() {
indx += direct;
if (indx <= 0){
indx = 0;
direct = direct * -1;
};
msg = 'indx = ' + indx + ' direct = ' + direct;
document.getElementById("msg").innerHTML = msg;
}

function test() {
alert('this is a test')
}

function handleButtonPress(e) {
if (e.target.id == "next") {
direct = 1;
}
else if (e.target.id == "prev") {
direct = -1
}
}

function displayMsg(msg) {
document.getElementById("msg").innerHTML = msg;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example Timeout + Buttons</title>

<!-- <script src="test.js"></script> Does not work here!! -->

</head>
<body>
<p id='msg'></p>
<p>
At start<br>
<button id="next">Next</button>
<button id="prev">Previous</button>
</p>

<!-- Script works here!! -->
<script src="test.js"></script>

</body>
</html>

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