我想复制列表中标签中的文本

时间:2019-05-15 09:22:37

标签: javascript django

步骤1 获取ID表单按钮

第2步 复制td中具有相同ID的文本

Codepen) https://codepen.io/terecal/pen/LoxmbP?editors=1010

    $(".copy_code").click(function(e){
        e.preventDefault();
        var id = this.id
        alert("id : ", id)
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
  <td>code</td>
  <td colspan="122" id="my_code_122"> hello world </td>
  </tr>  
</table>
<button type="button" name="button" id="122" class="copy_code">copy</button>

有可能吗?

p.s

我应用了您的方法。

以下代码可以正常工作

`

    $(".copy_code").click(function(e){
        e.preventDefault();
        var id = $(this).attr('id')
        alert("id : " + id)

        var text = document.getElementById(id),
        textVal = text.innerText;
        textTd = $(`#my_code_${id}`).text()

        alert("copy code " + textTd) // textVal  code copy

        // I want to copy this text as if pressing ctrl + c. Is there a way? Thank you if you let me know.
    });

`

问题几乎解决了。

我只想复制获得的文本,就像按Ctrl + C

你能告诉我如何吗?

ps2

当前代码是这个

    $(".copy_code").click(function(e){
        e.preventDefault();
        var id = $(this).attr('id')
        alert("id : " + id)

        var text = document.getElementById(id),
        textVal = text.innerText;
        textTd = $(`#my_code_${id}`).text()

        alert("copy code " + textTd) // textVal  code copy
        // I want to copy this text as if pressing ctrl + c. Is there a way? Thank you if you let me know.

        var dummy = document.createElement('textarea');
        dummy.value = textTd;
        //
        document.body.appendChild(dummy );
        dummy.select();
        //
        document.execCommand('copy');
        document.body.removeChild(dummy);

        alert("copy code2 " + textTd)
    });

复制通常是可行的

但在ctrl + v之后

这是新行(br)不起作用^^ ;;

original:
from django.contrib import admin
from django.urls import path, include
from . import views

app_name= 'css_challenge'
urlpatterns = [

]

copy:
 from django.contrib import adminfrom django.urls import path, includefrom . import views

app_name= 'css_challenge'urlpatterns = [

] 

是否可以应用新行?

3 个答案:

答案 0 :(得分:0)

也许您可以尝试:

$(".copy_code").click(function(e){
        e.preventDefault();
        var id = $(this).attr('id');
        alert(id)

        // var copyText = document.getElementById("my_code");
        // copyText.select();
        // document.execCommand("copy");
        // alert("Copied the text: " + copyText.value);

    });

答案 1 :(得分:0)

也许尝试一下。首先,您需要在警报中添加一个+而不是一个。其次,我不知道您是否特别需要复制功能,因为您想将文本粘贴到其他地方。 AFAIK,我只像在输入/文本区域上那样“复制”。我不确定其他元素是否支持该类型的API。无论如何,以下代码应获取文本内容。我还没有测试过,但是可以。

$(".copy_code").click(function(e){
        e.preventDefault();
        var id = this.id
        alert("id : "+ id)

        var text = document.getElementById(id),
            textVal = text.innerText;

        console.log(textVal);
    });

答案 2 :(得分:0)

您可以使用javascript将文本复制到剪贴板-有点不方便。文本必须是html元素的一部分。

首先创建一个虚拟文本元素

var dummy = document.createElement('textarea');

并为其提供变量的值

dummy.value = textTd;

将虚拟对象添加到DOM

document.body.appendChild(el);

选择

dummy.select();

最终将其文本内容复制到系统剪贴板

document.execCommand('copy');

并最终再次从DOM中删除创建的元素

document.body.removeChild(dummy);