gpt4 book ai didi

javascript - 如何写入特定区域/部门?

转载 作者:行者123 更新时间:2023-11-28 20:08:33 24 4
gpt4 key购买 nike

我的代码:

<button type="button" onclick="ayee()" value="click" />
<script>
function ayee(){
confirm("Ready to play?");
var age = prompt("How old are you?");
if (age >= 18) {
document.write("Let's get started then!");
}else{
document.write("You're under 18? Be careful out there....");
}
}
</script>

基本上,我使用 document.write() 来编写 JS 的响应,但我想将响应推送到某个分区或区域,这样我就可以保持页面其余部分的格式。

最佳答案

不要使用document.write 。相反,在您想要输出的位置创建一个 div 并为其指定一个 id:

<div id="result">I will be updated</div>

然后在您的 JS 代码中使用:

function ayee() {
confirm("Ready to play?");
var age = prompt("How old are you?"),
// Get a reference to the element we want to update
el = document.getElementById('result'),
message;

// Check the age and set the message variable based on that
if (age >= 18) {
message = "Let's get started then!";
} else {
message = "You're under 18? Be careful out there....";
}
// Update the content of the element with the message
el.innerHTML = message;
}

关于javascript - 如何写入特定区域/部门?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319063/

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