我正在编写一个点击游戏并使用javascript并尝试使用.innerHTML,但是它创建了一个错误,称为无法设置null的innerHTML,我已经在这个网站上发布了一段时间并找到了解决方法,比如把html之前的东西放进去js,但没有任何工作
<!DOCTYPE html>
<html>
<head>
<title>Prison Clicker</title>
<style>
</style>
</head>
<body>
<h1>Aquire</h1>
<p id="cigarets_show">0</p>
<button onClick="clicking()">CLICK</button>
<script>
var cigaretts = 0;
var cigarettsPC = 0;
function clicking(){
cigaretts += cigarettsPC;
}
setInterval(
function showvariables(){
var cigaretts_show = document.getElementById("cigaretts_show")
cigaretts_show.innerHTML = cigaretts;
}
)
</script>
</body>
答案 0 :(得分:1)
你有拼写错误。你也有没有时间的setInterval。
<!DOCTYPE html>
<html>
<head>
<title>Prison Clicker</title>
<style>
</style>
</head>
<body>
<h1>Aquire</h1>
<p id="cigarets_show">0</p>
<button onClick="clicking()">CLICK</button>
<script>
var cigaretts = 0;
var cigarettsPC = 1;
function clicking(){
cigaretts += cigarettsPC;
}
setInterval(
function showvariables(){
var cigaretts_show = document.getElementById("cigarets_show")
cigaretts_show.innerHTML = cigaretts;
} , 1000
)
</script>
</body>
&#13;
答案 1 :(得分:0)
您使用的ID不正确..因此显示错误
<!DOCTYPE html>
<html>
<head>
<title>Prison Clicker</title>
<style>
</style>
</head>
<body>
<h1>Aquire</h1>
<p id="cigarets_show">0</p>
<button onClick="clicking()">CLICK</button>
<script>
var cigaretts = 0;
var cigarettsPC = 0;
function clicking(){
cigaretts += cigarettsPC;
}
setInterval(
function showvariables(){
var cigaretts_show = document.getElementById("cigarets_show")
cigaretts_show.innerHTML = cigaretts;
}
)
</script>
</body>
答案 2 :(得分:0)
我会按如下方式简化您的代码:
var element = document.getElementById("count");
var count = 0;
function increment(){
count++;
element.innerText = count;
}
&#13;
<h1>Aquire</h1>
<p id="count">0</p>
<button onClick="increment()">CLICK</button>
&#13;