jQuery 表格行选择的 css 与需要分隔的 2 个不同背景颜色的行共享
嘿,当我单击一行时,我一直在试图弄清楚如何更改所选属性box-shadow。
我可以这样做:
$('#grdSocialMediaFeeds').on('click', 'tr', function () {
if ($(this)[0].style.backgroundColor == "") {
//the rows background is white
console.log("white");
$(this).css('box-shadow',"inset 0 0 2px 2px #a7957f"); //Greyish color
} else {
//the rows background is red.
console.log("red");
$(this).css('box-shadow',"inset 0 0 2px 2px #a92525"); //Dark red-ish color
}
});
将正确的颜色框阴影放在我单击的行周围,效果很好。
如果它是具有红色背景的行,那么当用户单击/选择该行时,我会在其周围放置较暗的红色框阴影。
如果它是具有白色背景的行,那么当用户单击/选择该行时,我会在其周围放置一个灰色框阴影排。
现在问题是,如果我去选择另一个单元格(白色或红色背景),那么之前单击的行仍然是视为仍被选中行,因此它的仍然具有框阴影,现在“真实”选定的行也应用了框阴影。
为了让事情变得更复杂(比实际应该的更复杂),白色背景行和红色背景行分享相同的tr.k-master-row.k-state-selected
CSS 属性。您可以使用 Shift+单击 或 Ctrl+单击选择多行 kbd> 意味着。
有没有通过jQuery的任何方法/技巧,我可以取消选择任何以前选择的行并让它只显示框- 当前用户行的阴影被选中?
Hey all I am stuck trying to figure out how to change the selected property box-shadow when I click on a row.
I could do this:
$('#grdSocialMediaFeeds').on('click', 'tr', function () {
if ($(this)[0].style.backgroundColor == "") {
//the rows background is white
console.log("white");
$(this).css('box-shadow',"inset 0 0 2px 2px #a7957f"); //Greyish color
} else {
//the rows background is red.
console.log("red");
$(this).css('box-shadow',"inset 0 0 2px 2px #a92525"); //Dark red-ish color
}
});
And that does work fine putting the correct color box-shadow around the row I clicked on.
If it was a row that has a red background then I would put a darker red box-shadow around it when the user clicked/selected that row.
If it was a row that has a white background then I would put a greyish box-shadow around it when the user clicked/selected that row.
Now the problem being that if I go and select another cell (white or red background) then the previously clicked row is still considered as still being the selected row and therefore its still has the box-shadow and now the "real" selected row also has the box-shadow applied to it.
To make things more complicated (than they really should be), both the white background and red background rows share the same tr.k-master-row.k-state-selected
css property. And you can select more than one row using the Shift+click or Ctrl+click means.
Is there any way/trick via jQuery that I can un-select whatever previous selected row(s) and have it only show the box-shadow on what the current user row(s) selected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经用简单的语言描述了解决方案,您只需要在 JS 中实现它即可! :-) 这是一个工作片段:
你确实应该使用 CSS,而不是内联样式,而且这样做会更简单,下面是如何做到这一点的示例:
是的,这让事情变得复杂了。然而,这里有很多关于如何做到这一点的示例 - 这是一个: 使用shift和鼠标单击选择多个元素 - jquery
因此使用这种方法:
如果是普通单击,首先删除所有
突出显示
的行,然后添加这highlighted
类到单击的行(我们已经完成了这部分);如果是按住 Shift 键单击,则将
highlighted
类添加到单击的行,而不删除任何当前突出显示;但是如果按住 Shift 键并单击时它已突出显示怎么办?在这种情况下,我们希望从单击的行中删除突出显示,而不删除任何其他当前突出显示;
这是一个工作片段,证明了这一点:
You've already described the solution in plain language, you just need to implement it in JS! :-) Here's a working snippet:
You really should be using CSS, rather than inline styles, and it is even a bit simpler when you do, here's an example of how you could do that:
Yep, that complicates things. However there are plenty of examples of how to do that here on SO - here's one: selecting multiple elements using shift and mouse click - jquery
So using that approach:
if it is a plain click, first remove all
highlight
ed rows, and then add thehighlighted
class to the clicked row (we've already done this part);if it is a shift-click, add the
highlighted
class to the clicked row, without removing any current highlights;but what if it is already highlighted on shift-click? In that case we want to remove the highlight from the clicked row, without removing any other current highlights;
Here's a working snippet demonstrating that: