簡體   English   中英

jQuery選擇器按類從集合中刪除子級

[英]JQuery Selector To Remove Children From Collection, By Class

這可能是一個簡單的問題,當我看到答案時,我會很愚蠢,但是我不是JavaScript專家,而且我已經為此苦苦掙扎了一段時間。 這個問題是基於我正在嘗試對該jQuery插件HTML-table-to-Excel導出庫進行的修改

該庫排除<tr>標記具有特定類的表行。 但是,我想添加功能,以便它將刪除具有該類的其他子元素,而不刪除整個行(根本的問題是,我在表格單元格中有一些按鈕,這些按鈕在Web瀏覽器中有意義,但在Excel中卻沒有) 。 這是本質上的問題。

HTML:

<div id="test-div">
<b>Original Table</b>
<table id="test-table">
<tr class="exclude"><td>Exclude this row</td></tr> <!-- this works fine-->
<tr><td>Include this whole row</td></tr>
    <tr><td>Include this row <span class="exclude">but not this text</span></td>  </tr>
</table>
    <hr />
    <b>Modified Table</b> <!-- is inserted by jQuery below -->  
</div>    

和Jquery:

$("#test-div").append("<table>");
$("#test-table").find("tr").not(".exclude").each(function (i,o) {
$("#test-div").append("<tr>" + $(o).html() + "</tr>"); //  <-- how to exclude child span of class "exclude" from this tr? 
   }); 
$("#test-div").append("</table>");

這是一個JSFiddle 目的是在不更改原始表(用戶正在查看)的情況下,使文本“但不跨度”不出現在“修改表”(即正在導出的內容)中。 如果span是距離較遠的孩子,或者它是另一種標簽類型(例如,如果它是<tr><td><div><div class="exclude"> ,則該div也應排除在外)也應該起作用)。 我試過鏈接各種其他.not()方法,或者不嘗試在另一個find()上使用::)選擇器,但是沒有運氣。

呢?

<div id="test-div">
    <b>Original Table</b>
    <table id="test-table">
    <tr class="exclude"><td>Exclude this row</td></tr>
    <tr><td>Include this whole row</td></tr>
    <tr><td>Include this row <span class="exclude">but not this text</span></td></tr>
</table>
    <hr />
     <b id="new-table">Modified Table</b>

</div>    

jQuery的

var newTable = $("#test-table").clone();
newTable.appendTo("#new-table").html();
$("#new-table  *").remove(".exclude");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM