简体   繁体   English

悬停一个div并在表的同一行中使用JS修改另一个div的CSS,该表中的3行使用相同的类

[英]Hover a div and modify the CSS of another div using JS inside of the same row in a table with 3 rows using same classes

I have a HTML table that includes 3 rows. 我有一个包含3行的HTML表。 Each row has a column circle and a column panel (sometimes the panel column is before the circle, sometimes after). 每行都有一个列圆圈和一个列面板(有时面板列在圆圈之前,有时在圆圈之后)。

    <table>
      <tr>
        <td>
          <div class="circle"></div>
        </td>
        <td>
          <div class="panel-right></div>
        </td>
      </tr>

      <tr>
        <td>
          <div class="panel-left></div>
        </td>
        <td>
          <div class="circle"></div>
        </td>
      </tr>

      <tr>
        <td>
          <div class="circle"></div>
        </td>
        <td>
          <div class="panel-right></div>
        </td>
      </tr>
    </table>

When hover panel, panel goes left or right from 10px. 当将鼠标悬停在面板上时,面板从10px开始向左或向右移动。

    table .panel-left:hover,
    table .panel-left:focus {
      margin-right: -10px;
      margin-left: 10px;
    }

    table .panel-right:hover,
    table .panel-right:focus {
      margin-left: -10px;
      margin-right: 10px;
    }

With javascript, i want to modify the css of circle when hover the panel-left or panel right 使用javascript,将鼠标悬停在左面板或右面板上时,我想修改圆形的css

    $(document).ready(function() {

      $(".panel-left").mouseover(function(){
        $(".circle").css("border", "5px solid #16a085");
      });

      $(".panel-left").mouseout(function(){
        $(".circle").css("border", "3px solid #cccccc");
      });

      $(".panel-right").mouseover(function(){
        $(".circle").css("border", "5px solid #16a085");
      });

      $(".panel-right").mouseout(function(){
        $(".circle").css("border", "3px solid #cccccc");
      });

    });

This is working well, except than all the circle are modified, when it should be only the circle inside of the row that contains the panel hovered. 效果很好,除了修改了所有圆之外,只有将包含面板的行内的圆悬停了。

What should be the javascript? javascript应该是什么?

Is this what you want?: 这是你想要的吗?:

HTML: HTML:

<table>
  <tr>
    <td class="circle-td">
      <div class="circle"></div>
    </td>
    <td>
      <div class="panel-right"></div>
    </td>
  </tr>

  <tr>
    <td>
      <div class="panel-left"></div>
    </td>
    <td class="circle-td">
      <div class="circle"></div>
    </td>
  </tr>

  <tr>
    <td class="circle-td">
      <div class="circle"></div>
    </td>
    <td>
      <div class="panel-right"></div>
    </td>
  </tr>
</table>


CSS: CSS:

.circle, .panel-right, .panel-left {
   background-color:red;
   width:200px;
   height:200px;
   border-radius:100%;
   border:3px solid #cccccc;
}

.panel-right, .panel-left {
    width:200px;
    height:200px;
    background-color:green;
}

.panel-left:hover,
.panel-left:focus {
  margin-right: -10px;
  margin-left: 10px;
}

.panel-right:hover,
.panel-right:focus {
  margin-left: -10px;
  margin-right: 10px;
}


Javascript 使用Javascript

$(document).ready(function() {
    $('.panel-left, .panel-right').mouseover(function() {
        $(this).closest('td')
               .siblings('.circle-td')
               .find('.circle')
               .css('border', '5px solid #16a085');
    }).mouseout(function() {
        $(this).closest('td')
               .siblings('.circle-td')
               .find('.circle')
               .css('border', '5px solid #cccccc')
    });   
});


And here is the fiddle 这是小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM