单击按钮时在剪贴板中复制textarea的文本

时间:2016-06-06 13:16:25

标签: javascript jquery copy textarea

我希望创建一个jQuery(或javascript) apply plugin: 'com.android.application' android { compileSdkVersion 23 //buildToolsVersion '23.0.2' // compileSdkVersion 23 buildToolsVersion '24.0.0-rc3' useLibrary 'org.apache.http.legacy' defaultConfig { applicationId "com.autoflik.cadlr" minSdkVersion 16 targetSdkVersion 23 versionCode 27 versionName "1.5" multiDexEnabled true jackOptions { enabled true } multiDexEnabled = true } dexOptions { incremental = true; preDexLibraries = false javaMaxHeapSize "4g" } compileOptions { sourceCompatibility = org.gradle.api.JavaVersion.VERSION_1_8 targetCompatibility = org.gradle.api.JavaVersion.VERSION_1_8 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } lintOptions { // if true, stop the gradle build if errors are found abortOnError false // if true, only report errors } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } lintOptions { abortOnError false } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile project(':multi-image-selector') compile project(':RangeBar') compile files('libs/gcm.jar') ////////tanuj compile 'com.android.support:appcompat-v7:24.0.0-alpha2' compile 'com.android.support:design:24.0.0-alpha2' compile 'com.android.support:support-v4:24.0.0-alpha2' compile 'com.android.support:mediarouter-v7:24.0.0-alpha2' compile 'com.google.android.gms:play-services:8.4.0' compile 'com.android.support:recyclerview-v7:24.0.0-alpha2' compile 'com.github.bumptech.glide:volley-integration:1.4.0' compile 'com.android.support:cardview-v7:24.0.0-alpha2' compile 'com.daimajia.swipelayout:library:1.2.0@aar' compile 'com.android.support:multidex:1.0.0' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.google.android.gms:play-services-ads:8.4.0' compile 'com.google.android.gms:play-services-auth:8.4.0' compile 'com.google.android.gms:play-services-gcm:8.4.0' // // compile 'com.android.support:appcompat-v7:23.1.1' // compile 'com.android.support:design:23.1.1' // compile 'com.android.support:support-v4:23.1.1' // compile 'com.android.support:mediarouter-v7:23.1.1' // compile 'com.google.android.gms:play-services:8.4.0' // compile 'com.android.support:recyclerview-v7:23.1.1' // compile 'com.github.bumptech.glide:volley-integration:1.4.0' // compile 'com.android.support:cardview-v7:23.1.1' // compile 'com.daimajia.swipelayout:library:1.2.0@aar' // compile 'com.android.support:multidex:1.0.0' // compile 'com.github.bumptech.glide:glide:3.7.0' } ,用于选择button中的所有内容,然后在您点击按钮时将文本复制到textarea

我找到了一些使用焦点事件的例子。但是我正在寻找一个你实际上必须点击进行选择和复制的按钮。

我该怎么做才能做到这一点?

4 个答案:

答案 0 :(得分:45)

您需要使用select()选择textarea的文字并使用execCommand('copy')来处理所选文字。它在浏览器的高级版本中工作。

$("button").click(function(){
    $("textarea").select();
    document.execCommand('copy');
});

你也可以在没有jquery的情况下完成这项工作,如底部

所示

document.querySelector("button").onclick = function(){
  document.querySelector("textarea").select();
  document.execCommand('copy');
};
<button>Select</button>
<br/>
<textarea></textarea>

答案 1 :(得分:7)

不使用jQuery就可以做到这一点。

这是一个纯粹的js解决方案。

function copy() {
  let textarea = document.getElementById("textarea");
  textarea.select();
  document.execCommand("copy");
}
<textarea id="textarea"></textarea>
<br>
<button onclick="copy()">Copy</button>

答案 2 :(得分:0)

**Copying text of textarea**
<textarea id="text" class="form-control" rows="21" cols="40" name="text">
                This word has two main meanings. The first has to do with being pleased and satisfied (feeling content) or making someone else feel happy and at peace with things (contenting them). The other meaning has to do with subject matter: the content of a history class might be American history. The content of a math class might be geometry. As long as there's a topic or subject, there's content.
</textarea>
**The following code added to script area**
$("button").click(function(){
      $("textarea").select();
      document.execCommand('copy');
      });

答案 3 :(得分:0)

当您的textarea元素由于某种原因而被禁用时,或者如果您不想看到所选文本的视觉效果,那么下面的解决方案将为您服务。

$("#button_id").click(function(){
    var $temp = $("<textarea></textarea>");
    $("body").append($temp);
    $temp.val($("#textarea_source").val()).select();     <-- #textarea_source: id of textarea source to be copied to the clipboard
    document.execCommand("copy");
    $temp.remove();
})
相关问题