簡體   English   中英

如何比較ClassName,以便刪除TD標簽?

[英]How do I compare ClassNames so that I can remove a TD tag?

我需要比較<TD><TR>元素的類名稱。 如果它們匹配,則我需要能夠在單擊TR時刪除TD 這是一些代碼:

 $("#button1").click(function() { $("table").find("tr:not(:nth-child(1))").remove(); }); $("tr").click(function() { $(this).append($("<td>", { text: $(this).attr("class") })); }); 
 body { display: flex; justify-content: center; align-items: center; } #table td { padding: 25px; font-size: 25px; text-align: center; } #table th { font-size: 30px; padding: 25px } div { display: block; } 
 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"> </head> <body> <div> <table border="1" id="table"> <tr class="class1"> <th>Year</th> <th>Savings</th> </tr> <tr class="class2"> <td>2014</td> <td>$10000</td> </tr> <tr class="class3"> <td>2015</td> <td>$8000</td> </tr> <tr class="class4"> <td>2016</td> <td>$9000</td> </tr> </table> <p> <input id="button1" type="button" value="Click to remove all rows except first one" /> </p> </div> </body> </html> 

現在,我可以使用它,以便單擊TR時可以添加TD ,但問題是它一直在添加TD 再次單擊TR后,我需要將其刪除。 為此,我需要比較新創建的TD和現有TR的類名。 如果它們匹配,我需要將其刪除。

使用.filter()查找文本與您單擊的tr的類匹配的td 如果找到任何一個,請將其刪除,否則添加一個新的。

 $("#button1").click(function() { $("table").find("tr:not(:nth-child(1))").remove(); }); $("tr").click(function() { var classname = $(this).attr("class"); var tr = $(this).find("td").filter(function() { return $(this).text() == classname; }); if (tr.length == 0) { $(this).append($("<td>", { text: classname })); } else { tr.remove(); } }); 
 body { display: flex; justify-content: center; align-items: center; } #table td { padding: 25px; font-size: 25px; text-align: center; } #table th { font-size: 30px; padding: 25px } div { display: block; } 
 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"> </head> <body> <div> <table border="1" id="table"> <tr class="class1"> <th>Year</th> <th>Savings</th> </tr> <tr class="class2"> <td>2014</td> <td>$10000</td> </tr> <tr class="class3"> <td>2015</td> <td>$8000</td> </tr> <tr class="class4"> <td>2016</td> <td>$9000</td> </tr> </table> <p> <input id="button1" type="button" value="Click to remove all rows except first one" /> </p> </div> </body> </html> 

根據您的描述,您甚至不需要比較類。 這很方便,因為您從未將類分配給新單元格。

而是根據單元格計數顯示和隱藏單元格

 $("#button1").click(function() { $("table").find("tr:not(:nth-child(1))").remove(); }); $("tr").click(function() { //If we have 2 cell add if($(this).find("td").length === 2) { $(this).append($("<td>", { text: $(this).attr("class") })); //Otherwise remove }else{ $(this).find("td:last-child").remove(); } }); 
 body { display: flex; justify-content: center; align-items: center; } #table td { padding: 25px; font-size: 25px; text-align: center; } #table th { font-size: 30px; padding: 25px } div { display: block; } 
 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"> </head> <body> <div> <table border="1" id="table"> <tr class="class1"> <th>Year</th> <th>Savings</th> </tr> <tr class="class2"> <td>2014</td> <td>$10000</td> </tr> <tr class="class3"> <td>2015</td> <td>$8000</td> </tr> <tr class="class4"> <td>2016</td> <td>$9000</td> </tr> </table> <p> <input id="button1" type="button" value="Click to remove all rows except first one" /> </p> </div> </body> </html> 

這是一種解決方法。 我使用隱藏/顯示<TD>而不是類比較。

 $("#button1").click(function() { $("table").find("tr:not(:nth-child(1))").remove(); }); $(".class2").click(function() { $(".TD2").toggleClass("show"); }); $(".class3").click(function() { $(".TD3").toggleClass("show"); }); $(".class4").click(function() { $(".TD4").toggleClass("show"); }); 
  body { display: flex; justify-content: center; align-items: center; } #table td { padding: 25px; font-size: 25px; text-align: center; } #table th { font-size: 30px; padding: 25px } div { display: block; } .TD2, .TD3, .TD4 { display: none; } .show { display: block; } 
 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"> </head> <body> <div> <table border="1" id="table"> <tr class="class1"> <th>Year</th> <th>Savings</th> </tr> <tr class="class2"> <td>2014</td> <td>$10000</td> <td class="TD2">class2</td> </tr> <tr class="class3"> <td>2015</td> <td>$8000</td> <td class="TD3">class3</td> </tr> <tr class="class4"> <td>2016</td> <td>$9000</td> <td class="TD4">class4</td> </tr> </table> <p> <input id="button1" type="button" value="Click to remove all rows except first one" /> </p> </div> </body> </html> 

暫無
暫無

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

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