JSFiddle示例可在Web上使用,但不适用于我的PC

Itarachiu

我在谷歌上看一个脚本来复制输入字段,这遇到了:

https://jsfiddle.net/9q3c1k20/

我的例子:

    document.getElementById("copy-text").onclick = function() {
      this.select();
      document.execCommand('copy');
      alert('This is a test...');
    }
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script src="http://code.jquery.com/jquery-1.11.0.js"></script>
    </head>
    
    <body>
    <input id="copy-text" type="text" value="Select a part of this text!">
    </body>
    </html>

一定的表现

脚本选择#copy-text正在分析文档正文(和输入元素)之前运行。将脚本移到正文的底部:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
</head>

<body>
<input id="copy-text" type="text" value="Select a part of this text!">
<script>
document.getElementById("copy-text").onclick = function() {
  this.select();
  document.execCommand('copy');
  alert('This is a test...');
}
</script>
</body>
</html>

但是将您的脚本放在单独的.js文件中并为其指定defer属性更为优雅

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script src="myscript.js" defer></script>
</head>
<body>
<input id="copy-text" type="text" value="Select a part of this text!">
</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

$(this)选择器可在jsfiddle中使用,但不适用于我的脚本

html2canvas 适用于 jsfiddle 但不适用于我的网站

可在jsfiddle中使用,但不适用于html页面

omouseover虽然适用于在线图像,但不适用于我的机器上的图像

wcf https 适用于我的本地电脑,但不适用于 android

MKAnnotation setImage可在iPhone上使用,但不适用于iPad / iPhone 6+

Bootstrap导航栏collpase按钮可在PC上使用,但不适用于笔记本电脑或手机

尝试使用正则表达式来验证模型中的字段。适用于Rubular,但不适用于我的验证

华硕电池健康充电功能可在Ubuntu 19.10上使用,但不适用于Ubuntu 20.04

Jsoup HTML解析可在Java上运行,但不适用于android studio

AWS CLI命令可在Bash上运行,但不适用于PHP shell_exec()

Photon PUN2 Unity3D - 发送 RPC 可在独立 (PC) 上运行,但不适用于 Android

我的where子句不适用于我的结果

MySQL查询可在MySQL中使用,但不适用于Java

编码新功能,可在代码块中使用,但不适用于vscode

SQL Query可在SQL Developer中使用,但不适用于Java端

Python多重处理可在Linux中使用,但不适用于Windows

通过引用传递可在C中使用,但不适用于此代码的C ++

使用Qt进行多处理可在Windows中运行,但不适用于Linux

阵列比较可在Chrome中使用,但不适用于Chrome IPAD

删除Ajax可在localhost中使用-但不适用于生产托管

动画屏幕可在手机上使用,但不适用于平板电脑

RegEx替换可在Ruby gsub中使用,但不适用于sed

SQL查询可在phpMyAdmin中使用,但不适用于php页面

Alexa函数可在AWS lamda中使用,但不适用于Service Simulator

RegExp可在Perl,JavaScript和Objective-C中使用,但不适用于Java

MySQL INSERT查询可在Phpmyadmin中使用,但不适用于PHP

Javascript可在JS Fiddle中使用,但不适用于Chrome / IE

medialement.js 不适用于我在 iphone 上的 hls