简体   繁体   English

通过jQuery隐藏第二个表行中的span

[英]Hide span in second table row through jQuery

I have a table that is generated in flask: 我有一个在烧瓶中生成的表:

<table class="table" id="preferencesTable">
  <thead>
    <th>
      <span>Fall 2016 Course Preferences</span>
      <span id="addPreference" class="glyphicon glyphicon-plus-sign pull-right"></span>
    </th>
  </thead>
  <tbody>
    {% for row in data.course_preferences %}
        <tr id="{{row.id}}">
          <td>
            {{ row.name }}
            <span class="pull-right">
              <span class="glyphicon glyphicon-arrow-up icon_padding"></span>
              <span class="glyphicon glyphicon-arrow-down icon_padding"></span>
              <span class="glyphicon glyphicon-remove icon_padding></span>
              <span class="glyphicon glyphicon-remove icon_padding></span>
            </span>
          </td>
        </tr>
    {% endfor %}
  </tbody>
</table>

I want to have the up and down arrows move the order of the table rows. 我想让向上和向下箭头移动表格行的顺序。 Because the first table row (not including the header) is at the top, the up arrow is not needed. 由于第一个表行(不包括标题)位于顶部,因此不需要向上箭头。 I want to hide this arrow. 我想隐藏这个箭头。

I am able to find that span with the following: 我能够找到以下内容:

$('#preferencesTable tr:nth-child(2) span')[2]

which returns 返回

<span class=​"glyphicon glyphicon-arrow-down icon_padding">​::before​</span>​

But I lose all of the abilities to show and hide the span (.css, .hide, etc. are no longer valid). 但是我失去了显示和隐藏跨度的所有能力(.css,.hide等不再有效)。

How can I hide this span only on the first row? 如何才能在第一行隐藏此跨度?

You need to access the first child under the tbody of the table, then you can find the span with class glyphicon-arrow-up and hide it. 你需要根据访问的第一个孩子tbody表,那么你可以找到span带班glyphicon-arrow-up和隐藏。 Also using [2] to access the span will return a dom element reference not a jQuery object so you won't be able to call any jQuery methods using it. 同样使用[2]访问span将返回dom元素引用而不是jQuery对象,因此您将无法使用它调用任何jQuery方法。

The tr:nth-child(2) will select the tr which is the second child of its parent, in this case it will select the second tr in the tbody tr:nth-child(2)将选择tr作为其父级的第二个子级,在这种情况下,它将选择tbody的第二个tr

$('#preferencesTable tbody tr:first-child span.glyphicon-arrow-up').hide()

The answer given by Arun P Johny seems to be correct. Arun P Johny给出的答案似乎是正确的。

I have additionaly created a jsfiddle: Click here to see the demo 我另外创建了一个jsfiddle: 点击这里查看演示

You can put this JS inside an event handler so that on some action it always hide the up arrow for the first row. 您可以将此JS放在事件处理程序中,以便在某些操作中它始终隐藏第一行的向上箭头。

$('selector').eventName(function(){
      $('#preferencesTable tbody tr:first-child span.glyphicon-arrow-up').hide();
});

I believe the problem is that when you use the square bracket operators, you get a vanilla JavaScript DOM object. 我相信问题在于,当你使用方括号运算符时,你会获得一个vanilla JavaScript DOM对象。 Refer to: https://learn.jquery.com/using-jquery-core/faq/how-do-i-pull-a-native-dom-element-from-a-jquery-object/ 请参阅: https//learn.jquery.com/using-jquery-core/faq/how-do-i-pull-a-native-dom-element-from-a-jquery-object/

You can solve it by wrapping that DOM object in $() to get a jQuery object, as follows: 您可以通过在$()包装该DOM对象来获取jQuery对象来解决它,如下所示:

var firstRowSpan = $('#preferencesTable tr:nth-child(2) span')[2];
firstRowSpan = $(firstRowSpan);
firstRowSpan.hide();

See also: convert DOM to jQuery 另请参阅: 将DOM转换为jQuery

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

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