Javascript 如何向特定区域/部门写信?
我的代码:Javascript 如何向特定区域/部门写信?,javascript,html,document.write,Javascript,Html,Document.write,我的代码: <button type="button" onclick="ayee()" value="click" /> <script> function ayee(){ confirm("Ready to play?"); var age = p
<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>
函数ayee(){
确认(“准备好玩了吗?”);
var age=prompt(“你多大了?”);
如果(年龄>=18岁){
文档。写下(“让我们开始吧!”);
}否则{
记录。写下(“你不满18岁?在外面要小心……”);
}
}
基本上,我使用document.write()来编写JS的响应,但我想将响应推送到某个分区或区域,这样我就可以保留页面其余部分的格式。不要使用
文档。编写。相反,创建一个div,将输出放在其中,并给它一个id:
我将被更新
然后在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;
}
不要使用文档。编写。相反,创建一个div,将输出放在其中,并给它一个id:
我将被更新
然后在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;
}
首先在要写入数据的分区上放置一个id属性
<div id="div-id"></div>
首先在要写入数据的分区上放置一个id属性
<div id="div-id"></div>
你能解释一下el和s以及el.innerHTML=s的作用吗?这些都是非常基本的东西,我已经用解释更新了帖子。你能解释一下el和s以及el.innerHTML=s的作用吗?这些都是非常基本的东西,我用解释更新了帖子。