2015-12-21 90 views
-1

我需要改進複選框的onclick函數。當點擊複選框contenteditable單元格應該是劃線。我怎樣才能做到這一點?感謝所有迴應。
我簡單的代碼如下像:調用值onclick複選框Javascript

<!DOCTYPE html> 
<html> 
<head> 

<style> 
    table { 
     border-collapse: collapse; 
     margin: 100px; 
    } 
    table, td, th { 
     border: 1px solid black; 
     padding: 10px; 
    } 
     table td.crossed { 
      background-image: linear-gradient(to bottom right, transparent calc(50% - 1px), red, transparent calc(50% + 1px)); 
     } 
</style> 
</head> 
<body> 
    <table id="t1"> 
    <tr> 
     <th></th> 
     <th></th> 
     <th></th> 
     <th></th> 
    </tr> 
    <tr> 
     <td>1.</td> 
     <td> 
      <input type="checkbox"> 
     </td> 
     <td> 
      <input type="date" id="mydate"> 
     </td> 
      <td contenteditable='true'></td> 
    </tr> 
</table> 

<script> 

</script> 
</body> 
</html> 

There should be like this picture

+1

你的javascript代碼在哪裏? –

回答

1

缺乏的類或ID,您可以使用querySelector才能獲得相應的元素:

var cb = document.querySelector('input[type="checkbox"]'); 
var td = document.querySelector("td[contenteditable]"; 

cb.addEventListener("click", function(){ 
    td.className = td.className + " crossed"; 
}); 

這將僅適用於工作> IE8

+2

這會更容易做'querySelector()'而不是'querySelectorAll()[0]' –