简体   繁体   English

如何通过单击连续更改跨度的颜色和文本?

[英]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.有几个问题。

  • The test for what text is in the span is being done on the element itself, not on its text.对跨度中的文本的测试是在元素本身上完成的,而不是在其文本上。
  • the updating of color needs to be done on the span element.颜色的更新需要在 span 元素上完成。

 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.

相关问题 使用jquery点击文本后,如何更改文本的颜色? - How do I change color of a text after clicking on it using jquery? 如何在单击时更改跨度文本的颜色,并在再次单击时恢复正常? - How do I change the color of the span text when it's clicked, and back to normal when clicked again? 单击跨度元素后如何允许用户更改跨度文本内容? - How do I allow a user to change span textcontent after clicking on the span element? 单击特定按钮时,如何更改跨度的颜色 - How do I change the color of a span, when a specific button is clicked 如何在单击时更改按钮颜色,glyphicon和文本? - How can I change button color,glyphicon and text on clicking it? 单击按钮后如何更改按钮的背景颜色? - How do I change the background color of button after clicking on it? 如何更改另一个跨度内按钮的跨度颜色 - How do I change the color of span inside a button inside another span 如何更改鼠标悬停时的span标签的颜色,但仅更改鼠标悬停时的span标签? - how do i change the color of a span tag on mouse over, but only the span tag that the mouse is over? 如何在 JavaScript 中更改 span 元素的文本? - How do I change the text of a span element in JavaScript? 如何使用 JavaScript 更改跨度元素的文本? - How do I change the text of a span element using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM