我无法将确认框与段落标记链接

时间:2018-04-01 09:38:57

标签: javascript html

如果用户在确认框中单击“确定”,他可以访问网站,否则他将被拒绝。如何将innerHtml文本传递给段落标记?



// Confirm Box
if(confirm("Please confirm that you are a citizen of India.") == true)
{ document.getElementbyId("demo").innerHTML = "Welcome to our Website!";
}
else
{ document.getElementbyId("demo").innerHTML = "Access Denied";
}

<!DOCTYPE html>
<html>
<head>
<title>PromptBox, ConfirmBox, AlertBox</title>
</head>
<body>

<p id="demo" ></p>
</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

试试这个。您犯的错误是您在b中输入B而不是document.getElementById("")。请记住,javascript是一种区分大小写的语言。

&#13;
&#13;
// Confirm Box
if(confirm("Please confirm that you are a citizen of India.") == true)
{
document.getElementById("demo").innerHTML = "Welcome to our Website!";
}
else
{
document.getElementById("demo").innerHTML = "Access Denied";
}
&#13;
<!DOCTYPE html>
<html>
<head>
<title>PromptBox, ConfirmBox, AlertBox</title>
</head>
<body>

<p id="demo"></p>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

<script>
(function() {
    if (confirm("Please confirm that you are a citizen of India.") == true) {
        document.getElementbyId("demo").innerHTML = "Welcome to our 
        Website!";
    } else {
        document.getElementbyId("demo").innerHTML = "Access Denied";
    }

})();
</script>