Мой код:
<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 get started then!");
}else{
document.write("You're under 18? Be careful out there....");
}
}
</script>
В основном, im, используя document.write(), чтобы написать ответ JS, но я хочу направить ответ на определенный раздел или область, поэтому я могу сохранить формат остальной части страницы.
Не используйте document.write
. Вместо этого создайте div, в котором вы хотите, чтобы выход был отправлен, и дайте ему идентификатор:
<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 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;
}
Сначала поместите атрибут id в раздел, для которого вы хотите записать данные
<div id="div-id"></div>
Затем используйте следующую строку, чтобы изменить ее содержимое.
document.getElementById("div-id").innerHTML="Text you want tot write here"