Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

javascript - Copy each td cell text to your clipboard with a button

I am trying to set a button to each td text in the table that will copy the contents to your clipboard.

I want to select the node contents of each text using jquery's this keyword. When I pass in the jQuery object it will throw an error.

It was working when I passed a unique ID, but when I passed a jQuery object it did not work. How can I achieve this efficiently?

$(document).ready( function() {
   $('input[data-target]').on("click", function(){
      var docSelector = document.createRange();      
      var get_text = $(this);
      console.log(get_text);
      console.log(docSelector.selectNodeContents(get_text));
     
      var selection = window.getSelection();
      selection.removeAllRanges();
      selection.addRange(docSelector);

      document.execCommand("copy");
      selection.removeAllRanges();
      
   });   
});
table, th, td{
  border: 1px solid #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
  <table>
    <thead>
     <tr>
       <th>Copy</th>
       <th>Text</th>
     </tr>     
     <tr>
       <td> <input type="button" data-target='copy' value="X"></td>
       <td id='copy'> Copy some text </td>
     </tr>
     <tr>
       <td> <input type="button" data-target='copy1' value="X"></td>
       <td id='copy1'> Copy some more text </td>
     </tr>
     <tr>
       <td> <input type="button" data-target='copy2' value="X"></td>
       <td id='copy2'> Copy some even more text </td>
     </tr>
    </thead>
  </table>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to get the text of adjacent cell:

var get_text = $(this).closest('td').next()[0];

The snippet:

$('input[data-target]').on("click", function(){
    var docSelector = document.createRange();
    var get_text = $(this).closest('td').next()[0];
    console.log(get_text);

    var selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(docSelector);

    document.execCommand("copy");
    selection.removeAllRanges();

});
table, th, td{
    border: 1px solid #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
    <thead>
    <tr>
        <th>Copy</th>
        <th>Text</th>
    </tr>
    <tr>
        <td> <input type="button" data-target='copy' value="X"></td>
        <td id='copy'> Copy some text </td>
    </tr>
    <tr>
        <td> <input type="button" data-target='copy1' value="X"></td>
        <td id='copy1'> Copy some more text </td>
    </tr>
    <tr>
        <td> <input type="button" data-target='copy2' value="X"></td>
        <td id='copy2'> Copy some even more text </td>
    </tr>
    </thead>
</table>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...