这是我的代码:
<!DOCTYPE HTML>
<html>
<head>
<title>John's Sustainability Clicker</title>
<style>
h1 { padding: 0; margin: 10px 0 0; }
p { padding: 0; margin: 10px; }
</style>
</head>
<script type="text/javascript">
if (clicks = 50) {
window.alert "You have made earth sustainable!"
} else {
document.write " "
}
</script>
<div style="color:#AF7817">
<center><h1 stlye="margin-bottom: 0 0 0 0"><p style="font-size:70px">My Sustainability Clicker</p></h1></center>
</div>
<div style="color:#2B65EC">
<div class="game-object">
<script type="text/javascript">
var clicks = 0;
function updateClickCount() {
document.getElementById("clickCount").innerHTML = clicks;
}
</script>
<center><button type="button" onClick="clicks++;updateClickCount();" id="push">
<p style="font-size:20px"><div style="color:#E238EC">Click me for Trees!</div></p></button></center>
<center> <p style="font-size:20px"><div id="clickCount"></div></p></center>
</div>
</div>
<div style="color:#F70D1A">
<center><p style="font-size:20px"><i><b>By: John Parkinham</i></b></p></center>
</div>
<center><input type="image" name="Cookie" value="Cookie" src="tree-clipart-tree_tiny_green_shaded.png" width="445px"/></center>
</input>
我的问题是,我如何拥有它,以便当我的点击变量达到50时,我会在屏幕上弹出一条警告消息?我不知道是否应该使用if else命令或者是否应该使用其他东西。
由于 〜约翰
答案 0 :(得分:0)
你在正确的轨道上,认为你需要一个if语句。但哪里你需要一个if语句。当前if(clicks = 50)有几个问题。
首先,单个等号将为变量点击分配值50。要比较两个值然后返回true或false值,您需要使用==
第二段代码:
<script type="text/javascript">
if (clicks = 50) {
window.alert "You have made earth sustainable!"
} else {
document.write " "
}
</script>
在首次定义变量点击之前,将在您的页面中运行。每次在updateClickCount函数中更新该值时,检查点击次数值的正确位置
function updateClickCount() {
document.getElementById("clickCount").innerHTML = clicks;
if(clicks == 50)
{
alert("hurray 50!");
}
}
答案 1 :(得分:0)
<!DOCTYPE HTML>
<html>
<head>
<title>John's Sustainability Clicker</title>
<style>
h1 { padding: 0; margin: 10px 0 0; }
p { padding: 0; margin: 10px; }
</style>
</head>
<script type="text/javascript">
var clicks = 0;
function updateClickCount() {
clicks++;
document.getElementById("clickCount").innerHTML = clicks;
if (clicks == 50) {
alert( "You have made earth sustainable!");
}
}
</script>
<div style="color:#AF7817">
<center><h1 stlye="margin-bottom: 0 0 0 0"><p style="font-size:70px">My Sustainability Clicker</p></h1></center>
</div>
<div style="color:#2B65EC">
<div class="game-object">
<center><button type="button" onclick="updateClickCount();" id="push">
<p style="font-size:20px"><div style="color:#E238EC">Click me for Trees!</div></p></button></center>
<center> <p style="font-size:20px"><div id="clickCount"></div></p></center>
</div>
</div>
<div style="color:#F70D1A">
<center><p style="font-size:20px"><i><b>By: John Parkinham</i></b></p></center>
</div>
<center><input type="image" name="Cookie" value="Cookie" src="tree-clipart-tree_tiny_green_shaded.png" width="445px"/></center>
</input>
...编辑
答案 2 :(得分:0)
<!DOCTYPE HTML>
<html>
<head>
<title>John's Sustainability Clicker</title>
<style>
h1 { padding: 0; margin: 10px 0 0; }
p { padding: 0; margin: 10px; }
</style>
</head>
<script type="text/javascript">
</script>
<div style="color:#AF7817">
<center><h1 stlye="margin-bottom: 0 0 0 0"><p style="font-size:70px">My Sustainability Clicker</p></h1></center>
</div>
<div style="color:#2B65EC">
<div class="game-object">
<script type="text/javascript">
var clicks = 0;
function updateClickCount() {
document.getElementById("clickCount").innerHTML = clicks;
if (clicks == 50) {
alert("You have made earth sustainable!");
} else {
/// do something
}
}
</script>
<center><button type="button" onClick="clicks++;updateClickCount();" id="push">
<p style="font-size:20px"><div style="color:#E238EC">Click me for Trees!</div></p></button></center>
<center> <p style="font-size:20px"><div id="clickCount"></div></p></center>
</div>
</div>
<div style="color:#F70D1A">
<center><p style="font-size:20px"><i><b>By: John Parkinham</i></b></p></center>
</div>
<center><input type="image" name="Cookie" value="Cookie" src="tree-clipart-tree_tiny_green_shaded.png" width="445px"/></center>
</input>