简体   繁体   English

如何遍历具有特定img alt属性的表行并删除JavaScript中的单元格

[英]How do I iterate through table rows with specific img alt attribute and delete cells in javascript

Have an html table as follows (# of rows vary).. 有一个html表,如下所示(行数有所不同)。

 <table id="issuetable">
     <tbody>
       <tr id="issuerow13210" rel="13210" data-issuekey="Ticket-215" class="issuerow focused">
        <td class="issuetype"><a class="hidden-link issue-link" data-issue-key="Ticket-215" href="/browse/Ticket-215" tabindex="-1" title="Ticket-215"></a>     <a class="issue-link" data-issue-key="Ticket-215" href="/browse/Ticket-215"> <img src="/servicedesk/issue-type-icons?icon=it-help" height="16" width="16" border="0" align="absmiddle" alt="IT Help" title="IT Help - For general IT problems and questions."></a></td>
        <td class="issuekey">
          <a class="issue-link" data-issue-key="Ticket-215" href="/browse/Ticket-215">Ticket-215</a>
        </td>
        <a class="issue-link" data-issue-key="Ticket-215" href="/browse/Ticket-215"></a>
    <td class="issue_actions">    <div class="action-dropdown aui-dd-parent">
      <a class="aui-dropdown-trigger aui-dd-link icon-tools-small issue-actions-trigger trigger-happy" id="actions_13210" title="Actions (Type . to access issue actions)" href="/rest/api/1.0/issues/13210/ActionsAndOperations?atl_token=B3EQ-V14Z-C9T2-CSQ1|a8670a0d83de2caedc2f08b4935ffa3acc8d8488|lin">
       <span>
         <em>Actions</em>
       </span>
     </a>
   </div>
 </td>
    </tr>

    <tr id="issuerow13209" rel="13209" data-issuekey="Ticket-214" class="issuerow">
<td class="issuetype"><a class="hidden-link issue-link" data-issue-key="Ticket-214" href="/browse/Ticket-214" tabindex="-1" title="Ticket-214"></a>     <a class="issue-link" data-issue-key="Ticket-214" href="/browse/Ticket-214"> <img src="/servicedesk/issue-type-icons?icon=it-help" height="16" width="16" border="0" align="absmiddle" alt="Task" title="Task"></a></td>
<td class="issuekey">
  <a class="issue-link" data-issue-key="Ticket-214" href="/browse/Ticket-214">Ticket-214</a>
</td>
<td class="issue_actions">    <div class="action-dropdown aui-dd-parent">
  <a class="aui-dropdown-trigger aui-dd-link icon-tools-small issue-actions-trigger trigger-happy" id="actions_13209" title="Actions (Type . to access issue actions)" href="/rest/api/1.0/issues/13209/ActionsAndOperations?atl_token=B3EQ-V14Z-C9T2-CSQ1|a8670a0d83de2caedc2f08b4935ffa3acc8d8488|lin">
    <span>
      <em>Actions</em>
    </span>
  </a>
 </div>
 </td>
</tr>

</tbody>
     </table>

How can I use javascript once the page finishes loading to iterate through each row and delete the cell "issue_actions" only if the row contains an image with alt="IT Help"? 页面加载完成后,如何仅在该行包含带有alt =“ IT Help”的图像的情况下使用JavaScript才能遍历每一行并删除单元格“ issue_actions”?

使用伪选择器:has ,然后找到要删除的元素:

 $('tr:has(img[alt="IT Help"]) .issue_actions').remove();

My own suggestion would be: 我自己的建议是:

$('img[alt="IT Help"]').closest('td').siblings('.issue_actions').empty();

The use of .empty() removes the content of the <td> elements, but does not remove the <td> itself, which should prevent the table layout being distorted by rows with differing cell-counts. 使用.empty()会删除<td>元素的内容,但不会删除<td>本身,这应防止表布局被具有不同单元格计数的行扭曲。

 $('img[alt="IT Help"]').closest('td').siblings('.issue_actions').empty(); 
 img[alt="IT Help"] { box-shadow: 0 0 0 5px #f00; } td { border: 1px solid #000; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr> <td> <img src="http://placekitten.com/150/150/" alt="IT Help" /> </td> <td class="issue_actions">This shouldn't be visible</td> </tr> <tr> <td> <img src="http://lorempixel.com/150/150/people" alt="Not IT Help" /> </td> <td class="issue_actions">This should be visible</td> </tr> <tr> <td> <img src="http://placekitten.com/150/150/" alt="IT Help" /> </td> <td class="issue_actions">This shouldn't be visible</td> </tr> </tbody> </table> 

The following uses remove() , which demonstrates the slight visual problem: 以下使用remove() ,它演示了轻微的视觉问题:

 $('img[alt="IT Help"]').closest('td').siblings('.issue_actions').remove(); 
 img[alt="IT Help"] { box-shadow: 0 0 0 5px #f00; } td { border: 1px solid #000; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr> <td> <img src="http://placekitten.com/150/150/" alt="IT Help" /> </td> <td class="issue_actions">This shouldn't be visible</td> </tr> <tr> <td> <img src="http://lorempixel.com/150/150/people" alt="Not IT Help" /> </td> <td class="issue_actions">This should be visible</td> </tr> <tr> <td> <img src="http://placekitten.com/150/150/" alt="IT Help" /> </td> <td class="issue_actions">This shouldn't be visible</td> </tr> </tbody> </table> 

References: 参考文献:

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

相关问题 如何遍历 JavaScript 中的表格行和单元格? - How do I iterate through table rows and cells in JavaScript? 如何遍历JavaScript中的表行和单元格并添加值? - How do I iterate through table rows and cells in JavaScript and add the values? 使用JavaScript迭代表格以获取特定单元格的值 - Iterate through a table using JavaScript to get values of specific cells 如何使用javascript删除特定的表格行? - How to delete specific table rows with javascript? Javascript / JQuery遍历表行和单元格,并将选中的复选框的属性输出到控制台 - Javascript/JQuery iterate through table rows and cells and output an attr of checked checkboxes to console 如何通过Javascript显示输入列表行? - How do I display input column table rows through Javascript? Javascript:如何遍历值数组并创建表行 - Javascript: How to iterate through an array of values and create table rows 如何从 mysql 表中获取所有行,发送 json,遍历数组并显示? - How do I get all the rows from a mysql table, send json, iterate through array and display? jQuery:遍历2个特定单元格的表行 - JQuery: Iterating through table rows for 2 specific cells 使用Javascript / Jquery遍历表行和复选框,如果选中,则将复选框的属性添加到数组 - Use Javascript/Jquery to iterate through table rows and checkboxes and add attribute of checkbox to array if checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM