2012-11-29 36 views
-1

iam不理解如何在點擊textarea後立即複製到clickboard ..我的意思是它應該選擇它裏面的所有內容,然後它應該彈出並且問一些'press ctrl c'複製到剪貼板..onclick全選textarea

我已經有,但不能選擇文本區域全文,並應複製到剪貼板代碼..

<!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代碼(不是J​​Query的)

+1

這是幹什麼的:'eval(「document.getElementById('showthis')。value」);'? –

+0

它顯示你的textarea的全文。你究竟在做什麼? – polin

+0

@Asad它沒有在textarea中選擇全文,也沒有得到該剪貼板彈出.. – Otero

回答

-1

使用jQuery,這將只是:

$('#showthis').select() 

只需使用JavaScript的:

document.getElementById('showthis').select() 
+0

我只需要使用Javascript ...不jQuery .. – Otero

1

試試這個代碼在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> 

如果你需要選擇文本並將其複製到剪貼板,我認爲你應該爲此目的的插件。看看這個問題:: Copy text to the client's clipboard using jQuery

+0

我應該只使用Javascript .. – Otero

0

在我開發了以下example你的代碼的基礎上,希望這將是有益的:

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); 
    }) 
}) 
+0

我只需要使用Javscreipt ... – Otero

+0

好吧,替換選擇器$()document.getElementById();) – Warlock