gpt4 book ai didi

javascript - 无法设置点击另一个单元格的 TD 的原始颜色

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:26 25 4
gpt4 key购买 nike

我有下面的html

JS fiddle : http://jsfiddle.net/klbaiju/97oku7mb/3/

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script type='text/javascript'>
$(window).load(function(){
$(document).ready(function () {

$('body').on('click', '.anchor3', function () {
var bcolor = $(this).parent().css("background-color");
$("a.anchor3:contains('B')").each(function () {
var pcolor = $(this).parent().css("background-color");
if (pcolor != "rgb(218, 112, 214)") {
$(this).parent().css("background-color", "red");
}
else {

$(this).parent().css("background-color", "Orchid");
}
});
$("a.anchor3:contains('b')").parent().css('background-color', 'LightGrey');
$(this).parent().css('background-color', 'Grey');



});
});
});

</script>


</head>

<body>
<table id="GridView3" cellspacing="0" border="1" border-collapse:collapse; rules="All" ;>
<tbody>
<tr>
<th>ID </th>
<th>01 </th>
<th>02 </th>
<th>03 </th>
<th>04 </th>
<th>05 </th>
<th>06 </th>
<th>07 </th>
</tr>
<tr>
<td>101</td>
<td style="background-color: Orchid;"><a class="anchor3" href="#">B</a></td>
<td style="background-color: rgb(255, 0, 0);"><a class="anchor3" href="#">B </a></td>
<td style="background-color: rgb(255, 0, 0);"><a class="anchor3" href="#">B </a></td>
<td style="background-color: rgb(255, 0, 0);"><a class="anchor3" href="#">B </a></td>
<td style="background-color: Orchid;"><a class="anchor3" href="#">B</a></td>
<td style="background-color: rgb(255, 0, 0);"><a class="anchor3" href="#">B </a></td>
<td style="background-color: rgb(255, 0, 0);"><a class="anchor3" href="#">B </a></td>
</tr>
</tbody>
</table>

</body>

</html>

有两个要求:

a) 如果我们按下任何单元格,它的颜色将变为灰色(有效)

b) 如果我们按下任何其他单元格,最后一个单元格应更改为其之前的颜色。意思是,假设我们按下了原始颜色为 ORCHID 的 TH = 01 单元格。所以它会是灰色的。现在,如果我们按下说 TH =04 单元格,该单元格背景颜色将为灰色,但 TH = 1 单元格颜色应为 ORCHID。目前它是红色的。

我需要做什么改变?

最佳答案

您可以在呈现页面时向单元格添加颜色类,而不是将背景颜色添加为内联样式,然后稍后只需切换灰色类。

$(document).ready(function () {
$('body').on('click', '.anchor3', function () {
$("a.anchor3:contains('B')").parent().removeClass('grey') ;
$(this).parent().addClass('grey');
});
});
.orchid {
background-color: rgb(218, 112, 214);
}
.red {
background-color: rgb(255, 0, 0);
}
.grey {
background-color: rgb(128, 128, 128);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<table id="GridView3" cellspacing="0" border="1" border-collapse:collapse; rules="All" ;>
<tbody>
<tr>
<th>ID </th>
<th>01 </th>
<th>02 </th>
<th>03 </th>
<th>04 </th>
<th>05 </th>
<th>06 </th>
<th>07 </th>
</tr>
<tr><td>101</td><td class="orchid">
<a class="anchor3" href="#">B</a>
</td><td class="red">
<a class="anchor3" href="#">B </a>
</td><td class="red">
<a class="anchor3" href="#">B </a>
</td><td class="red">
<a class="anchor3" href="#">B </a>
</td><td class="orchid">
<a class="anchor3" href="#">B</a>
</td><td class="red">
<a class="anchor3" href="#">B </a>
</td><td class="red">
<a class="anchor3" href="#">B </a>
</td></tr>
</tbody>
</table>

关于javascript - 无法设置点击另一个单元格的 TD 的原始颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39970331/

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