-1

テキストエリアをクリックするとすぐにクリックボードにコピーする方法を理解していません..つまり、その中のすべてのコンテンツを選択する必要があり、ポップして「ctrl cを押す」などの質問をしてクリップボードにコピーします..

私はすでにコードを持っていますが、テキスト領域で全文を選択できず、クリップボードにコピーする必要があります..

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
 <script type="text/javascript">

function myfunc2() {
 var selectedobj=document.getElementById('showthis');

  if(selectedobj.className=='hide'){  //check if classname is hide 
    selectedobj.style.display = "block";
    selectedobj.readOnly=true;
    selectedobj.className ='show';
  }else{
    selectedobj.style.display = "none";
    selectedobj.className ='hide';
 }
}


function copyToClipboard (text) {
  window.prompt ("Copy to clipboard: Ctrl+C, Enter", text);
}



function select_all()
{
// alert(document.getElementById("showthis").value);

var text_val=eval("document.getElementById('showthis').value");
text_val.focus();

var copy = text_val.select();
window.prompt ("Copy to clipboard: Ctrl+C, Enter", copy);

}

</script>
 </head>

 <body>


            <label  onclick="myfunc2()">Click here</label>
            <textarea id="showthis" style="display:none" class="hide"  onclick="select_all()" readonly>dfdsfsfasdfdsfsfasdfssdfsfasf</textarea>


 </body>
</html>


誰でもこれを調べてもらえますか...

編集: Javascript コードのみが必要です (JQuery ではありません) 。

4

3 に答える 3

2

このコードを試して、TextBox または TextArea 内のテキストを選択してください:

<textarea id="txtSelect">Hello</textarea>

<script type="text/javascript">
    var textBox = document.getElementById("txtSelect");
    textBox.onfocus = function() {
        textBox.select();

        // Work around Chrome's little problem
        textBox.onmouseup = function() {
            // Prevent further mouseup intervention
            textBox.onmouseup = null;
            return false;
        };
    };
</script>

テキストを選択してクリップボードにコピーする必要がある場合は、その目的のためのプラグインが必要だと思います。この質問を見てください :: jQueryを使用してクライアントのクリップボードにテキストをコピーしてください

于 2012-11-29T10:21:54.383 に答える
1

あなたのコードに基づいて、次のを開発しました。参考になることを願っています。

HTML:

<textarea id="showthis" class="hide" readonly>click to copy</textarea>

JS :

$(function(){
    var select_all = function(control){
        $(control).focus().select();
        var copy = $(control).val();
        window.prompt ("Copy to clipboard: Ctrl+C, Enter", copy);
    }
    $("#showthis").click(function(){
        select_all(this);
    })
})
于 2012-11-29T10:27:44.687 に答える
-1

JQuery を使用すると、次のようになります。

$('#showthis').select()

JavaScriptのみを使用:

document.getElementById('showthis').select()
于 2012-11-29T10:14:10.283 に答える