如何将某个对象里的内容全部复制到剪切板?就像论坛提交的时候,把帖子内容复制到剪切板。万一没发成功,可以粘贴。

解决方案 »

  1.   

    <input type="text" id="test" value="content">
    <input type="button" value="copy" onclick="copyText(document.getElementById('test'))">
    <script>
    function copyText(obj) {
    obj.select();
    clipboardData.setData("text",obj.createTextRange().text);
            alert(clipboardData.getData("text"));
    }
    </script>
      

  2.   

    给你一段js代码吧.刚给另外一个人弄的<!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>无标题文档</title>
    </head><body>
    <label name="testtext" id="testtext" onclick="copycode(this);">test copy functionsdfsdf sdfsd
    </label>
    <script type="text/javascript">function copycode(obj) { 
        var rng = document.body.createTextRange(); 
        rng.moveToElementText(obj); 
        rng.scrollIntoView(); 
        rng.select(); 
        rng.execCommand("Copy"); 
        window.alert("代码已复制到剪贴板中!"); 
        rng.collapse(false); 
    }
    </script></body>
    </html>
      

  3.   

    嗯,这个比我那个简单,实用
    附测试代码:<!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>无标题文档</title>
    </head><body>
    <script type="text/javascript">function copyText(obj) {
        obj.select();
        clipboardData.setData("text",obj.createTextRange().text);
            alert(clipboardData.getData("text"));
    }
    </script>
    <input id="content" name="content"  type="text" />
    <input name="btnSubmit" type="submit" onclick="javascript:copyText(document.getElementById('content'));" value="提交" />
    </body>
    </html>UP