点击复制文本并在之后显示小弹窗——扩展代码

3
我可以帮您翻译以下内容,涉及IT技术相关。"Original Answer" 可以翻译为 "最初的回答"。以下是需要翻译的内容(请保留HTML标签):

我需要一个小脚本,允许我在点击时复制文本字符串而无需按钮

我找到了这段代码:

function copy(that){
var inp =document.createElement('input');
document.body.appendChild(inp)
inp.value =that.textContent
inp.select();
document.execCommand('copy',false);
inp.remove();
}

这段代码可以在点击时复制文本,但没有任何提示。我想稍微修改一下,当用户点击文本时,它将被复制,并显示一个小弹出窗口,持续2-3秒钟(然后应该自动消失),提示文本已经被复制到剪贴板。有没有人有什么想法如何修改这个代码? <p onclick="copy(this)">example text</p> - 这是它识别要复制的代码的方式。请帮忙改进。
"Original Answer"翻译成中文是"最初的回答"。

请确保将您的复制函数插入为JavaScript,而不是PHP。 - undefined
这里的代码是Javascript,jquery的functions.php用于定义PHP函数。这两种语言不兼容,因此会出现语法错误。Javascript需要添加在网站HTML的<script>标签中,或者在一个.js文件中,并在HTML中引用,类似于<script src="myScript.js"></script> - undefined
请更新您的问题,包括您的functions.php文件的内容,这样我们才能帮助您调试。听起来您只是将那段代码粘贴到了那里,这可能解释了您所看到的错误,但除非我们看到代码,否则无法确定。 - undefined
谢谢大家,我错了。我把代码粘贴到single.php文件中的<script>标签中,它完美地运行了。现在我只需要修改代码来显示我提到的弹出窗口。有人有任何想法如何实现吗? - undefined
3个回答

0

我希望这个函数能对你有所帮助:

copy(){
input  = $(this).val();
document.execCommand('copy',false,input);

$(this).next('text copied');

setTimeout(function(){$(this).next().remove();}, 2000);
}

记住,你必须创建一个标签,在输入标签旁边显示消息。

谢谢!你能告诉我应该把代码粘贴在哪里吗?也许我应该将它与我提供的那个混合在一起吗?对不起,问题有点傻,我还在学习中。 - undefined
没有问题,您应该在导入jQuery库并关闭body标记后将它复制到页面的页脚中。 - undefined
嗯,我还是不太明白“在页脚引入jQuery库”这个部分。你能否给我展示一下完整代码应该是什么样的? - undefined

0
嘿,这里有一个完整的示例:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <input type="text" onclick="copy(this)" value="malek gorchene"></input>
    <!-- this p is for the notification -->
    <p></p>

</body>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script type="text/javascript">


    function copy(elem){
        input  = $(elem).val();
        elem.select();//Select the text in the input element 
        document.execCommand('copy');//copy it 

        $(elem).next().text('text copied');

        setTimeout(function(){$(elem).next().text('');}, 2000);//
    }


</script>
</html>

谢谢!:) 你发布的代码很棒,但我有两个问题:
  1. 目前它是这样的 - https://screenshots.firefox.com/M9QwnGKhmflTrD6L/test2.gromocje.pl - 我如何删除整个表单?我希望只有文本。
  2. 我希望每篇文章都有不同的文本可供复制。我在第一篇文章中发布的代码就是这样工作的 - 我将其粘贴到single.php中,然后我可以在WordPress文章编辑器中粘贴<p onclick="copy(this)">example text</p>并且每次都有不同的文本。有办法让它像那样工作吗?
- undefined

0
嘿,这是第一个问题,但我不明白第二个问题是什么。

function copy(elem){
  if($(elem).text()){
    var dummy = document.createElement("textarea");
      document.body.appendChild(dummy);
      dummy.value = $(elem).text();
      dummy.select();
      document.execCommand("copy");
      document.body.removeChild(dummy);
  }else{
    input  = $(elem).val();
    elem.select();//Select the text in the input element 
    document.execCommand('copy');//copy it 
  }



  $(elem).next().text('text copied');

  setTimeout(function(){$(elem).next().text('');}, 2000);//
}
<input type="text" onclick="copy(this)" value="click me"></input>
<p></p>
<p onclick="copy(this)">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<!-- this p is for the notification -->
<p></p>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>


谢谢!现在它完美地运行了。我成功地自己修改了它(第二个问题)。我还有最后两个问题:
  1. 当我想要点击复制代码时,我只需将以下代码粘贴到文章编辑器中:<p onclick="copy(this)">示例文章</p>。问题是,<p>标签会使文本跳到下一个段落。我希望没有间隙,所以我尝试使用<b>标签,但不幸的是,当我这样做时,代码停止工作了。你有任何办法可以让它按照我需要的方式工作吗(文本需要与其他句子在同一段落中)?
- undefined
  1. 我该如何为弹出窗口添加样式,以显示文本已被复制(添加背景、更改颜色)?我尝试在这里添加一个类:<script class="example-class" type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script><script type="text/javascript"> 但是它并不起作用 :/
- undefined
当我回家的时候,我会把代码发给你。但是如果你想提升自己的技能,你应该尝试阅读文档哦 :p - undefined
谢谢!我等不及了 :) 我经常这样做,但有时候对我来说最好的学习方式是在已有的代码上进行实验。:-) - undefined
随你喜欢,兄弟 ;) - undefined
嘿兄弟!你在家吗?:) - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接