为什么我得到无法设置null的属性(设置' innerhtml')
我正在制作赌博游戏,当您按下蓝色的按钮时,我正在尝试,png变成了绿色。这也给我带来了一个错误的错误,有没有办法将其带走?我知道OnClick可以在图像上工作,所以我不知道为什么它在按钮上不起作用,还有另一个属性会处理同一件事吗?
let tokens = 100
let left = 1
let middle = Math.floor(Math.random() * 10);
let right = Math.floor(Math.random() * 10);
document.getElementById("tokens").innerHTML = formatTokens(tokens);
function formatTokens(num, digits = 1) {
var si = [{
value: 1,
symbol: ""
},
{
value: 1E3,
symbol: " K"
},
{
value: 1E6,
symbol: " Million"
},
{
value: 1E9,
symbol: " Billion"
},
{
value: 1E12,
symbol: " trillion"
},
{
value: 1E15,
symbol: " Quadrillion"
},
{
value: 1E18,
symbol: " Quintillion"
},
{
value: 1E18,
symbol: " Quintillion"
},
{
value: 1E21,
symbol: " Sextillion"
}
];
var rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
var i;
for (i = si.length - 1; i > 0; i--) {
if (num >= si[i].value) {
break;
}
}
return (num / si[i].value).toFixed(digits).replace(rx, "$1") + si[i].symbol;
}
function gamble() {
if (left == 1) {
document.getElementById("green.png").innerHTML = "<img src='green.png' />";
console.log("hi")
}
}
<body>
<div class="sectionLeft">
<center>
<div class="tokenshower">
<span id="tokens">100</span> Tokens<br>
</div>
<br>
<input type="number" size="100">
<div class="leftdiamond">
<img src="blue.png" width="100" height="100">
</div>
<div class="middlediamond">
<img src="red.png" width="100" height="100">
</div>
<div class="rightdiamond">
<img src="green.png" width="100" height="100">
</div>
<button onclick="gamble()" height="1000000px" width=2 00px>daddy</button>
<img id="blue" src="blue.png">
</body>
I am making a gambling game and I am trying that when you hit the button the blue.png turns into the green.png but it tells me that I have an Unchaught TypeError. It is also giving me an error at the onclick, is there a way to take this away? I know that onclick works on images so I don't know why it doesn't work on buttons, is there another property that will do about the same thing?
let tokens = 100
let left = 1
let middle = Math.floor(Math.random() * 10);
let right = Math.floor(Math.random() * 10);
document.getElementById("tokens").innerHTML = formatTokens(tokens);
function formatTokens(num, digits = 1) {
var si = [{
value: 1,
symbol: ""
},
{
value: 1E3,
symbol: " K"
},
{
value: 1E6,
symbol: " Million"
},
{
value: 1E9,
symbol: " Billion"
},
{
value: 1E12,
symbol: " trillion"
},
{
value: 1E15,
symbol: " Quadrillion"
},
{
value: 1E18,
symbol: " Quintillion"
},
{
value: 1E18,
symbol: " Quintillion"
},
{
value: 1E21,
symbol: " Sextillion"
}
];
var rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
var i;
for (i = si.length - 1; i > 0; i--) {
if (num >= si[i].value) {
break;
}
}
return (num / si[i].value).toFixed(digits).replace(rx, "$1") + si[i].symbol;
}
function gamble() {
if (left == 1) {
document.getElementById("green.png").innerHTML = "<img src='green.png' />";
console.log("hi")
}
}
<body>
<div class="sectionLeft">
<center>
<div class="tokenshower">
<span id="tokens">100</span> Tokens<br>
</div>
<br>
<input type="number" size="100">
<div class="leftdiamond">
<img src="blue.png" width="100" height="100">
</div>
<div class="middlediamond">
<img src="red.png" width="100" height="100">
</div>
<div class="rightdiamond">
<img src="green.png" width="100" height="100">
</div>
<button onclick="gamble()" height="1000000px" width=2 00px>daddy</button>
<img id="blue" src="blue.png">
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
脚本必须在“令牌”元素加载后运行。可以通过将
onload
处理程序注册到window
来完成,也可以在元素之后放置脚本。The script must run AFTER the "tokens" element loads. This can be done either by registering an
onload
handler towindow
, or put the script after the element.您正在使用getElementById,但没有ID集。尝试为IMG设置ID。示例:
&lt; img src =“ green.png” id =“ green” width =“ 100”高=“ 100”&gt;
You're using getElementById but there is no id set. Try setting an id for the img. Example:
<img src="green.png" id="green" width="100" height="100">
您可以在没有一个ID中添加一个ID。
getElementById
否则无法使用。或者,如果您不想这样做,请选择
src
属性和值:这将返回带有该属性值的 first 元素。
You could add an ID to that element, which doesn't have one.
getElementById
can't work otherwise.Or, if you don't want to do that, select for the
src
attribute and value:This will return the first element with that attribute value.