简体   繁体   English

如何将带有输入文本的表格数据作为没有 jQuery 的单元格复制到剪贴板?

[英]How to copy table data with input text as cell without jQuery to ClipBoard?

I have a simple table of a format similar to following:我有一个类似于以下格式的简单表格:

<table>
  <thead>
    <tr>
      <th>Savings</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Savings 
         <button type="button" (click)="submit()">Click Here</button>
      </td>
      <td *ngFor="let data in items"><input type="text" [value]="data.savings"></td>
    </tr>
    <tr>
     <td> [Similar to above row]</td>
    </tr> 
  </tbody>
</table>

How do I properly copy the table with input values without jQuery maintaining row structure?如何在没有 jQuery 维护行结构的情况下正确复制具有输入值的表? I only care about rows data and not headers or cell name.我只关心行数据,而不关心标题或单元格名称。 I consulted Copy table rows to clipboard- copying only the first page but it copies the structure of data and not the input field value?我咨询了将表格行复制到剪贴板 - 仅复制第一页,但它复制数据结构而不是输入字段值?

Are you saying getting the value of the input box in each row of the table?你是说获取表格每一行输入框的值? Can be obtained with pure javascript:可以用纯javascript获取:

<script>
     var  values=document.getElementsByTagName("input");
     alert(values[0].value);
</script>

//document.getElementsByTagName (name) :Find elements by tag name //document.getElementsByTagName(name) :按标签名查找元素

This can be copied:这可以复制:

<script type="text/javascript">
function copy()
{
 var  values=document.getElementsByTagName("input");
 values[0].select(); // Select object
 document.execCommand("Copy"); //Execute browser copy command
 alert("Has been copied, can be pasted");
}
</script>
<input type="button" onClick="copy()" value="Click Copy Code" />

copy multiple lines:复制多行:

<script type="text/javascript">
function copy()
{

    var  values=document.getElementsByTagName("input");
    var textarea =document.createElement('textarea'); // create  textarea label
    //Traverse the content of the table and splice it into the content of the textarea
    for(var i=0;i<values.length; i++){
         textarea.innerHTML= textarea.innerHTML+=values[i].value+'\n'
    }
    document.body.appendChild(textarea) // Add to body  
    textarea.select();    
    document.execCommand("Copy"); //Execute browser copy command
    textarea.remove(); 
    alert("Has been copied, can be pasted");
}
</script>
<input type="button" onClick="copy()" value="Click Copy Code" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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