gpt4 book ai didi

javascript - 改变元素的背景颜色

转载 作者:行者123 更新时间:2023-11-30 20:49:55 25 4
gpt4 key购买 nike

我有多个同一个类的 td,我想根据它们是进行中还是已完成来更改背景颜色...

问题是 $('.isCompleted').text() 给了我所有的进行中(大字符串重复同样的事情)

我的代码不起作用

Uncaught TypeError: isCompleted.text is not a function
    at HTMLTableCellElement. (list-flows.js:4)
    at Function.each (jquery-3.2.1.min.js:2)
    at HTMLDocument. (list-flows.js:3)
    at j (jquery-3.2.1.min.js:2)
    at k (jquery-3.2.1.min.js:2)

$(document).ready(function () {
var isCompletedRows = $('.isCompleted');
$.each(isCompletedRows, function (index, isCompleted) {
console.log(isCompleted.text());
if(isCompleted.text() === "In Progress") {
$(this).closest('tr').css('background-color','#000');
}
else if(isCompleted.text() === "Completed") {
$(this).closest('tr').css('background-color','#555231');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<c:forEach var="flow" items="${flows}">
<tr class="flow-list">
<c:if test="${flow.isCompleted == 1}">
<td class="isCompleted">Completed</td>
</c:if>
<c:if test="${flow.isCompleted != 1}">
<td class="isCompleted">In Progress</td>
</c:if>
</tr>
</c:forEach>

最佳答案

因为 isCompleted 是 DOM,而不是 jQuery。

所以你需要做

$(isCompleted).text()

关于javascript - 改变元素的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48285460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com