简体   繁体   中英

Strikethrough Table and/or DIV

I currently utilized this method Linethrough/strikethrough a whole HTML table row and it works great.

However how do I attach it to a HTML check box onclick method?

http://jsfiddle.net/deaconf19/DrEhv/

<table border 1>
<tr>
<td>Status</td>
<td>Priority</td>
<td>Date</td>
<td>Event</td>
<td>Updated</td>
</tr>
<tr>
<td contenteditable/>Checkbox</td>
<td contenteditable/>3</td>
<td contenteditable/>02/07/2014</td>
<td contenteditable/>code needs updating again</td>
<td contenteditable/>02/07/2014</td>
</tr>
<tr class="strikeout">
<td contenteditable/>Checkbox</td>
<td contenteditable/>1</td>
<td contenteditable/>02/07/2014</td>
<td contenteditable/>code needs updating</td>
<td contenteditable/>02/07/2014</td>
</tr>
</table>

You can use jQuery, to find check box parents parent and add a class to it when status of checkbox changed

if ( this.checked) {
  $(this).parent().parent().addClass("strikeout");
} else {
 $(this).parent().parent().removeClass("strikeout");
}

Example

Just use javascript to change the table class. If using jQuery:

$('#myCheckbox').change(function(){
    $('#myTableRow').toggleClass('strikeout', $(this).prop('checked'));
})

The text below assumes that you have a checkbox with id="myCheckbox" and a table row with id="myTableRow" .

Why use jQuery if pure JS can do the same thing:

In your HTML, add onclick handler for checkboxes:

<input type="checkbox" onclick="strike(this)">

Then change class of TR element based on checkbox value, like this:

function strike(elm) {
    if(elm.checked) {
        elm.parentNode.parentNode.className = "strikeout";
    } else {
        elm.parentNode.parentNode.className = "";
    }
}

As simple as that !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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