简体   繁体   English

将鼠标悬停在多个tds上时,如何更改它们的类别?

[英]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')
    }
);

Working Demo 工作演示

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.

相关问题 将鼠标悬停在另一类上时如何更改它的CSS? - How to change css of one class when hovering over another? 如何在悬停在另一个不相关类的子类上时更改一个类的css? - How to change css of one class when hovering over child class of another unrelated class? 将鼠标悬停在一个 div 上时,如何将 class 添加到另一个 div? - How do add a class to another div, when hovering over one div? Javascript问题:将鼠标悬停在div上时如何从颜色数组更改标题的随机颜色? - Javascript problem: How do I change random color of heading from array of colors when hovering over div? 将鼠标悬停在其父div上时,如何更改链接为对象的SVG的属性? - How do I change properties of an SVG linked as object when hovering over its parent div? 在Highstocks中,将鼠标悬停在图表上时如何更改默认的时间/日期标签? - In Highstocks, how do I change the default time/day labels when hovering over graph? Javascript:将鼠标悬停在一个元素上时,如何定位幻灯片中的选定元素? - Javascript: How do i target selected elements in a slideshow when hovering over one element? 将鼠标悬停在下拉菜单中时,如何仅选择一列? - How do i select only one column when hovering over it in a drop down jQuery menu? 将鼠标悬停在链接上时如何更改图像? - How can I change the image when hovering over a link? jquery:hover 悬停其中一个时更改两个元素 - jquery :hover change two elements when hovering one of them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM