[英]How do I change the class of several tds when hovering over one of them?
here's my problem. 这是我的问题。 I have a month-view type calendar in a table. 我在表中有一个月视图类型的日历。 In this calendar there can be certain periods which are (they have the class 'blocked'). 在此日历中,可能有某些期间(它们的类别为“已阻止”)。 The first date of such a period has the class 'blocked' as well as 'start', while the last date has the classes 'blocked' and 'end'. 此期间的第一个日期的类别为“阻止”和“开始”,而最后一个日期的类别为“阻止”和“结束”。
Now, when I hover over one of these TDs I want that td, and all the other ones in ONLY THAT SPECIFIC period (can be over multiple rows), to get the class 'blocked-highlight'. 现在,当我将鼠标悬停在这些TD之一上时,我想要那个td以及仅在特定时间段内的所有其他TD(可以跨越多行),以获取“ blocked-highlight”类。
Here is a JSFiddle which generally shows what I have: http://jsfiddle.net/aoxmpouw/ 这是一个JSFiddle,它通常显示我拥有的东西: http : //jsfiddle.net/aoxmpouw/
Here is the Code from the Fiddle: 这是小提琴中的代码:
RELEVANT PART OF HTML: HTML的相关部分:
...
<td>13</td>
<td>14</td>
</tr>
<tr>
<td>15</td>
<td>16</td>
<td class="blocked start">17</td>
<td class="blocked">18</td>
<td class="blocked">19</td>
<td class="blocked">20</td>
<td class="blocked">21</td>
</tr>
<tr>
<td class="blocked">22</td>
<td class="blocked">23</td>
<td class="blocked end">24</td>
<td>25</td>
<td>26</td>
<td>27</td>
...
JS: JS:
$( document ).ready(function() {
$("td.blocked").hover(
function() {
$( this ).addClass('blocked-highlight')
}, function() {
$( this ).removeClass('blocked-highlight')
}
);
});
CSS: CSS:
.blocked {
color: white;
background-color: red;
}
.start {
border-left: 5px solid black;
}
.end {
border-right: 5px solid black;
}
.blocked-highlight {
background-color: blue;
}
JQuery is perfectly fine, as well as pure CSS solutions, if there are any. 如果有的话,jQuery和纯CSS解决方案都很好。
Thanks in advance, Cheers -N 在此先感谢,干杯-N
If you have multiple periods / blocks and they are generated dynamically. 如果您有多个期间/块,并且它们是动态生成的。 give them a shared attribute or class. 给他们一个共享的属性或类。 Then you could do something like: 然后,您可以执行以下操作:
HTML: HTML:
...
<td>13</td>
<td>14</td>
</tr>
<tr>
<td>15</td>
<td>16</td>
<td class="blocked start" rel="1">17</td>
<td class="blocked" rel="1">18</td>
<td class="blocked" rel="1">19</td>
<td class="blocked" rel="1">20</td>
<td class="blocked" rel="1">21</td>
</tr>
<tr>
<td class="blocked" rel="1">22</td>
<td class="blocked" rel="1">23</td>
<td class="blocked end" rel="1">24</td>
<td>25</td>
<td>26</td>
<td>27</td>
...
JS: JS:
$( document ).ready(function() {
$("td.blocked").hover(
function() {
rel = $(this).attr('rel');
$( "td[rel='"+rel+"']").addClass('blocked-highlight');
}, function() {
rel = $(this).attr('rel');
$( "td[rel='"+rel+"']").removeClass('blocked-highlight');
}
);
});
Demo: http://jsfiddle.net/aoxmpouw/5/ 演示: http : //jsfiddle.net/aoxmpouw/5/
you need to use .siblings()
along with .addBack()
to target sibling blocked and hovered element: 您需要使用.siblings()
和.addBack()
来定位被阻塞和悬停的兄弟元素:
$("td.blocked").hover(
function() {
$( this ).siblings('.blocked').addBack(".blocked").addClass('blocked-highlight')
}, function() {
$( this ).siblings('.blocked').addBack(".blocked").removeClass('blocked-highlight')
}
);
Is this wat you are expecting? 这是您期望的水吗? http://jsfiddle.net/aoxmpouw/1/ http://jsfiddle.net/aoxmpouw/1/
$( document ).ready(function() {
$("td.blocked").hover(
function() {
$('.blocked' ).addClass('blocked-highlight')
}, function() {
$('.blocked').removeClass('blocked-highlight')
}
);
});
i hope this will help you
just replace your js like this
$( document ).ready(function() {
$("td.blocked").hover(
function() {
$("td.blocked").addClass('blocked-highlight')
}, function() {
$("td.blocked").removeClass('blocked-highlight')
}
);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.