Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何更改innerHTML+;1._Javascript - Fatal编程技术网

Javascript 如何更改innerHTML+;1.

Javascript 如何更改innerHTML+;1.,javascript,Javascript,我想更改innerHTML,这样对于每个相同的圆,它都会添加+1 if (document.getElementById("circle1").style.backgroundColor == document.getElementById("circle5").style.backgroundColor) { document.getElementById("point2").innerHTML = +1 } if (document.getElementById("circle2"

我想更改innerHTML,这样对于每个相同的圆,它都会添加+1

if (document.getElementById("circle1").style.backgroundColor == document.getElementById("circle5").style.backgroundColor) {
    document.getElementById("point2").innerHTML = +1
}

if (document.getElementById("circle2").style.backgroundColor == document.getElementById("circle6").style.backgroundColor) {
    document.getElementById("point2").innerHTML = +1
}
我建议你用这个。首先获取旧值并转换为数字,然后加1并替换旧值。例如:

if (document.getElementById("circle1").style.backgroundColor == document.getElementById("circle5").style.backgroundColor) {
    let obj = document.getElementById("point2");
    let oldValue = Number(obj.innerText);
    obj.innerText = oldValue + 1;
}

if (document.getElementById("circle2").style.backgroundColor == document.getElementById("circle6").style.backgroundColor) {
    let obj = document.getElementById("point2");
    let oldValue = Number(obj.innerText);
    obj.innerText = oldValue + 1;
}

请参阅
innerText
innerHtml

文档。getElementById(“point2”)。innerHtml
不提供数字,因此无法向其中添加1。首先需要将内容解析为一个数字(如使用parseIntnumber),然后可以添加1

//创建引用变量(用于较小的代码)
var point2=document.getElementById(“point2”).innerHTML
if(document.getElementById(“circle1”).style.backgroundColor==
document.getElementById(“circle5”).style.backgroundColor){
document.getElementById(“point2”).innerHTML=increment(point2)
}
if(document.getElementById(“circle2”).style.backgroundColor==
document.getElementById(“circle6”).style.backgroundColor){
document.getElementById(“point2”).innerHTML=increment(point2)
}
//函数以增加点2的值
函数增量(html){
返回编号(document.getElementById(“point2”).innerHTML)+1
}
#圆圈1{
背景色:红色;
}
#圆圈5{
背景色:红色;
}
#圆圈2{
背景色:红色;
}
#圆圈6{
背景色:红色;
}
C1
碳五

C2 C6
0
document.getElementById(“point2”).innerHTML+=“+1”
。如果你想附加字符串,那么它会告诉我+1而不是2,我希望它是怎样的…你的意思是像计数器一样吗?所以当两者都是相同的时候,numbre是2same@Dario这里有什么<代码>文档.getElementById(“point2”).innerHTML
?你期望得到什么样的结果?