簡體   English   中英

如何在不弄亂邊框的情況下在表格行上添加刪除線

[英]how to make a strikethrough on a table row withot messing the borders

我需要在整個表格行上添加刪除線。

刪除線應該可以工作,所以每當我點擊表格行內的一個按鈕時,它就會運行一個 Jquery 函數: $(this).closest('tr').toggleClass('strikethrough');

名為刪除線的類應該具有使刪除線的屬性。

我試過這個:

strikethrough {
content: " ";
    position: absolute;
    top: 50%;
    left: 0;
    border-bottom: 1px solid #111;
    width: 100%;
}

但它只是弄亂了整個表格。

這是我的代碼:

 const animals = [{ "animalId": "1", "animalName": "elephent", "cageNum": "231", "legsNum": "4", "CandidatesForDeletion": false }, { "animalId": "2", "animalName": "tiger", "cageNum": "324", "legsNum": "56.00", "CandidatesForDeletion": false }, { "animalId": "3", "animalName": "wolf", "cageNum": "414", "legsNum": "210.40", "CandidatesForDeletion": false } ] let tableBody = '<table id="table"><tr class="tr tr1"><td class="td1">animal Id</td><td class="td1">animal name</td><td class="td1">cage Number</td><td class="td1">legs Number</td><td class="td1">delete</td></tr>'; animals.forEach(function (d) { tableBody += `<tr class="tr tr2"> <td class="td2">${d.animalId}</td> <td class="td2">${d.animalName}</td> <td class="td2">${d.cageNum}</td> <td class = "td2">${d.legsNum}</td> <td class="td2 trash_button">click for strikethrough</td> </tr>`; }); $(document).ready(function () { $(document).on('click', '.trash_button', function () { $(this).closest('tr').toggleClass('strikethrough'); }); }); function CreateTableFromJSON() { $('#showData').html(tableBody); }
 #table { overflow-x: auto; overflow-y: auto; position: absolute; left: 5%; top: 30%; width: 90%; align-content: center; font-size:12px; border-collapse:collapse; border: 2px solid black; } .tr { font-weight:bold; border: 2px solid black; height: 17%; } .tr1 { background:#16A1E7; height: 80px; } .tr2 { background: #ffffff; transition: 0.4s; height: 80px; } .tr2:hover { background-color: cyan; transition: 0.4s; } .td1 { justify-items: center; align-items: center; text-align: center; color:white; border: 2px solid black; height: 17%; } .td2 { justify-items: center; align-items: center; text-align: center; color:black; border: 2px solid black; height: 17%; } .strikethrough { text-decoration: line-through; color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button tabindex="1" onclick="CreateTableFromJSON()" value="Create Table From JSON" style="border: 1px solid black">animals</button><p id="showData"></p>

修改 CSS 部分

#table td {
    position: relative;
}

tr.strikethrough td:before {
    content: " ";
    position: absolute;
    top: 50%;
    left: 0;
    border-bottom: 1px solid #111;
    width: 100%;
}

 const animals = [{ "animalId": "1", "animalName": "elephent", "cageNum": "231", "legsNum": "4", "CandidatesForDeletion": false }, { "animalId": "2", "animalName": "tiger", "cageNum": "324", "legsNum": "56.00", "CandidatesForDeletion": false }, { "animalId": "3", "animalName": "wolf", "cageNum": "414", "legsNum": "210.40", "CandidatesForDeletion": false } ] let tableBody = '<table id="table"><tr class="tr tr1"><td class="td1">animal Id</td><td class="td1">animal name</td><td class="td1">cage Number</td><td class="td1">legs Number</td><td class="td1">delete</td></tr>'; animals.forEach(function (d) { tableBody += `<tr class="tr tr2"> <td class="td2">${d.animalId}</td> <td class="td2">${d.animalName}</td> <td class="td2">${d.cageNum}</td> <td class = "td2">${d.legsNum}</td> <td class="td2 trash_button">click for strikethrough</td> </tr>`; }); $(document).ready(function () { $(document).on('click', '.trash_button', function () { $(this).closest('tr').toggleClass('strikethrough'); }); }); function CreateTableFromJSON() { $('#showData').html(tableBody); }
 #table { overflow-x: auto; overflow-y: auto; position: absolute; left: 5%; top: 30%; width: 90%; align-content: center; font-size:12px; border-collapse:collapse; border: 2px solid black; } .tr { font-weight:bold; border: 2px solid black; height: 17%; } .tr1 { background:#16A1E7; height: 80px; } .tr2 { background: #ffffff; transition: 0.4s; height: 80px; } .tr2:hover { background-color: cyan; transition: 0.4s; } .td1 { justify-items: center; align-items: center; text-align: center; color:white; border: 2px solid black; height: 17%; } .td2 { justify-items: center; align-items: center; text-align: center; color:black; border: 2px solid black; height: 17%; } #table td { position: relative; } tr.strikethrough td:before { content: " "; position: absolute; top: 50%; left: 0; border-bottom: 1px solid #111; width: 100%; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button tabindex="1" onclick="CreateTableFromJSON()" value="Create Table From JSON" style="border: 1px solid black">animals</button><p id="showData"></p>

暫無
暫無

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

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