[英]How do I change color and text of a span continuously by clicking on it?
I need to create a span, which shows the status of an activity and specific color.我需要创建一个跨度,它显示活动的状态和特定颜色。 As an example default color of the span is red and the text is "Accept".
例如,跨度的默认颜色为红色,文本为“接受”。 by clicking on it it needs to change its color to green and text to Accepted.
通过单击它,它需要将其颜色更改为绿色,并将文本更改为 Accepted。 If Status is "Accepted" it needs to change its text to "completed".
如果状态为“已接受”,则需要将其文本更改为“已完成”。 If the status is "completed", it needs to change its background color to yellow and text to "closed".
如果状态为“完成”,则需要将其背景颜色更改为黄色,将文本更改为“关闭”。
I've tried this:我试过这个:
var span = document.getElementById("stat"); function txtclrchan() { if (span === "Accept") { span.textContent = "Accepted"; this.style.backgroundColor = "#047611"; } else if (span === "Accepted") { span.textContent = "Completed"; this.style.backgroundColor = "#047611"; } else { span.textContent = "Closed"; this.style.backgroundColor = "#F1C40F"; } }
<span class="badge bg-danger" id="stat" onclick="txtclrchan()">Accept</span>
But it's not working.但它不起作用。 It doesn't execute if and else if parts.
它不执行 if 和 else if 部分。 It directly jumps to the else part.
它直接跳转到else部分。 How do I do that?
我怎么做?
span
can't be equal to "Accept"
. span
不能等于"Accept"
。 It's a DOM element, not a string.它是一个 DOM 元素,而不是字符串。 You want to compare
span.textContent
.您想比较
span.textContent
。 Also this
refers to function txtclrchan()
, it has no style property (just use span.style
). this
也是指function txtclrchan()
,它没有样式属性(只需使用span.style
)。
var span = document.getElementById("stat"); function txtclrchan() { const text = span.textContent; if (text === "Accept") { span.textContent = "Accepted"; span.style.backgroundColor = "#047611"; } else if (text === "Accepted") { span.textContent = "Completed"; span.style.backgroundColor = "#047611"; } else { span.textContent = "Closed"; span.style.backgroundColor = "#F1C40F"; } }
<span class="badge bg-danger" id="stat" onclick="txtclrchan()">Accept</span>
you have to check the textContent
of the span.您必须检查跨度的
textContent
。 and the this
keyword in the callback does not refer to span
.并且回调中的
this
关键字不引用span
。
var span = document.getElementById("stat"); function txtclrchan() { if (span.textContent === "Accept") { span.textContent = "Accepted"; span.style.backgroundColor = "#047611"; } else if (span.textContent === "Accepted") { span.textContent = "Completed"; span.style.backgroundColor = "#047611"; } else { span.textContent = "Closed"; span.style.backgroundColor = "#F1C40F"; } }
<span class="badge bg-danger" id="stat" onclick="txtclrchan()">Accept</span>
Span is an element so you need take innerText using span.innerText and also replace this with span .
Span 是一个元素,因此您需要使用span.innerText获取 innerText 并将其替换为span 。
var span = document.getElementById("stat"); function txtclrchan() { if (span.innerText === "Accept") { span.textContent = "Accepted"; span.style.backgroundColor = "#047611"; } else if (span.innerText === "Accepted") { span.textContent = "Completed"; span.style.backgroundColor = "#047611"; } else { span.textContent = "Closed"; span.style.backgroundColor = "#F1C40F"; } }
<span class="badge bg-danger" id="stat" onclick="txtclrchan()">Accept</span>
There are a couple of problems.有几个问题。
var span = document.getElementById("stat"); function txtclrchan(){ var text = span.textContent; if(text==="Accept") { span.textContent = "Accepted"; span.style.backgroundColor="#047611"; } else if(text==="Accepted"){ span.textContent = "Completed"; span.style.backgroundColor="#047611"; } else{ span.textContent = "Closed"; span.style.backgroundColor="#F1C40F"; } }
<span class="badge bg-danger" id="stat" onclick="txtclrchan();">Accept</span>
var span = document.getElementById("stat"); var span = document.getElementById("stat");
function txtclrchan() {
if (span.textContent === "Accept") {
span.textContent = "Accepted";
console.log(this)
span.style.backgroundColor = "#047611";
} else if (span === "Accepted") {
span.textContent = "Completed";
this.style.backgroundColor = "#047611";
} else {
span.textContent = "Closed";
span.style.backgroundColor = "#F1C40F";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.