【问题标题】:Need to remove the row highlight after select the next row选择下一行后需要去掉行高亮
【发布时间】:2019-02-11 17:28:54
【问题描述】:

当我点击下一个表格行时,我需要移除行突出显示。

当我使用下面的代码时,表格行会在单击齿轮图标中的菜单时突出显示。然后,当我单击另一个表格行或齿轮图标时,现有表格行突出显示不会被删除。任何人都可以向我提供有关如何解决此问题的建议。

    click: function () {
label: 'Delete LMD Definition',
icon: 'delete',
   $("table tbody").on("click", "tr", function () {
  $("tr.selected")  // find any selected rows
     .not(this)  // ignore the one that was clicked
     .removeClass("selected");  // remove the selection
   $(this).toggleClass("selected");  // toggle the selection clicked row
});
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="ember18549" class="ember-view content-table focus-group object-table container-view highlighted">
  <tbody>
    <tr id="ember18784" class="ember-view content-row body-row-view container-view" tabindex="0" aria-label="">
      <td id="ember19010" class="ember-view lmdd-menu actions container-view">
        <rs-icon id="ember19015" class="ember-view action-menu icon clickable-icon" title="Acciones y Transiciones" style="width: 1em; height: 1em; font-size: 20px">
          <icon glyph="action" class="action" style="font-size: 20px;">
          </icon>
        </rs-icon>
      </td>
      <td id="ember19021" class="ember-view content-data view view-core_component_data-view-mixin name">
      </td>
    </tr>
    <tr id="ember18784" class="ember-view content-row body-row-view container-view" tabindex="0" aria-label="">
      <td id="ember19010" class="ember-view lmdd-menu actions container-view">
        <rs-icon id="ember19015" class="ember-view action-menu icon clickable-icon" title="Acciones y Transiciones" style="width: 1em; height: 1em; font-size: 20px">
          <icon glyph="action" class="action" style="font-size: 20px;">
          </icon>
        </rs-icon>
      </td>
      <td id="ember19021" class="ember-view content-data view view-core_component_data-view-mixin name">
      </td>
    </tr>
  </tbody>
</table>

【问题讨论】:

  • 如果你使用类会容易得多。
  • 你必须使用 javascript 还是一个简单的 CSS 规则就足够了?
  • 也许你应该显示 模板 而不是渲染的 DOM 输出......
  • @epascarello。我已经加入了课程,但我没有得到
  • @Shilly,js 和 css 都可以

标签: javascript jquery ember.js ember-1


【解决方案1】:

这纯粹是jQuery,我强烈建议不要这样做。如果可以,您应该使用 ember。但是因为您已经为此使用了 jQuery 并且不显示您的 ember 代码,所以这是您最容易解决的问题。

只需为状态添加一个 css 类。然后在所有其他行上使用removeClass,在重点行上使用addClass

$(function() {
  $("table.content-table.highlighted tr.content-row").on("focusout", function() {
    $('table.content-table.highlighted tr.content-row').removeClass('my-line');
    $(this).addClass('my-line');
  });
});
.my-line {
  background: #FFFF99 none 0 0 repeat;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="content-table highlighted">
  <tbody>
    <tr class="content-row" tabindex="0" aria-label="">
      <td>
        FOO
      </td>
      <td>
        BAR
      </td>
    </tr>
    <tr class=" content-row" tabindex="0" aria-label="">
      <td>
        BAZ
      </td>
      <td>
        BAL
      </td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 它工作正常。但是在选择所有菜单的齿轮图标时突出显示会消失。当我单击菜单的齿轮图标时,我需要突出显示该行
  • 真的应该用 ember 实现这个。
  • 它工作得很好@lux。但是齿轮图标第一次没有出现。在那之后,所有的点击都可以完美地工作
  • 嗨@lux。我已经对此stackoverflow.com/questions/52224647/… 提出了新问题。请在空闲时间查看并建议我。
【解决方案2】:

如果您使用类和 css 规则,这将是非常简单的事情。对突出显示使用 CSS 悬停状态,并使用 clickc 为选择添加/删除类。

$("table tbody").on("click", "tr", function () {
  $("tr.selected")  // find any selected rows
     .not(this)  // ignore the one that was clicked
     .removeClass("selected");  // remove the selection
   $(this).toggleClass("selected");  // toggle the selection clicked row
})
table{
border-collapse: collapse;
}

table tbody td {
  border: 1px solid black;
  padding: 1em;
}

table tbody tr:hover {
  background-color: #CCC;
  cursor: pointer;
}


table tbody tr.selected {
  background-color: #9999AA;
}

table tbody tr.selected:hover {
  background-color: #BBB;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td>1</td><td>Pizza</td>
    </tr>
    <tr>
      <td>2</td><td>Taco</td>
    </tr>
    <tr>
      <td>3</td><td>Burger</td>
    </tr>
    <tr>
      <td>4</td><td>Salad</td>
    </tr>
</table>

【讨论】:

  • 我有两次点击功能,这里我提到了二次点击功能。所以它需要第二次进行此操作。
  • @ScottDon 不知道你在说什么,因为你的原始代码没有显示点击功能,只是一个 foucsout 事件。
  • 所以您根本不想通过点击进行选择?真的还是不知道你想要什么。
  • 我已经编辑了代码,请检查。我在点击功能中包含了你的代码。如此而已。它仅在第二次点击时运行良好。请给我一些其他建议。
  • 看来您正在将 click 函数绑定到 click 方法中,所以它当然会有问题。
【解决方案3】:

尝试使用

$("table.content-table").click(function(){
  $(this).toggleClass("highlighted");
});

每次点击此表时,都会添加或删除突出显示的类。

或者下面的代码,这样当你点击的那个表被选中时,每个表都会被取消选中。

$("table.content-table").click(function(){
  $("table.content-table").removeClass("highlighted");
  $(this).addClass("highlighted");
});

【讨论】:

    猜你喜欢
    • 2011-05-21
    • 2014-07-08
    • 2021-12-28
    • 2013-11-27
    • 2023-04-04
    • 1970-01-01
    • 2014-12-10
    • 2016-04-08
    相关资源
    最近更新 更多