如何使用js粘贴文本行? [英] How to paste a text line using js?

查看:188
本文介绍了如何使用js粘贴文本行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我只是使用js的execCommand方法来做一些简单的事情来执行系统复制,删除等命令.

我做了两个文本区域,其中一个是我输入一些文本,然后单击按钮执行复制,剪切等命令.
问题:::: --------
在此粘贴按钮不起作用.就像我从一个文本区域复制一些文本一样,它没有粘贴到其他文本区域.

我也想只为textareas选择全部.就像如果光标在textarea1上并且如果我在selectAll按钮上单击一样,它应该只选择textarea1内容.目前,它正在选择整个页面的内容.

以下是我的代码

Well guys, i was just doing a simple stuff of executing system copy, delete etc. commands using execCommand method of js.

I have made two textareas, in one i am inputting some text and by hitting on the buttons i am executing copy, cut etc. commands.
Problems::::----
In this the paste button is not working. Like i am copying some text from one textarea, it is not pasting into other textarea.

Also I want to do select all for the textareas only. Like if cursor is on textarea1 and if I am hitting on selectAll button it should only select the textarea1 content. Currently it is selecting the whole page content.

Following is my code

 <html>
 <head>
  <script language="javascript">
   function doCopy()
   {
       document.execCommand('Copy',false,0);
   }
   function doPaste()
   {
       document.execCommand('Paste');
   }
   function doCut()
   {
       document.execCommand('Cut',false,0);
   }
   function doSelectAll()
   {
       document.execCommand('SelectAll',false,0);
   }
   function doDelete()
   {
       document.execCommand('Delete',false,0);
   }
   function doUndo()
   {
       document.execCommand('Undo',false,0);
   }
   function doUnselect()
   {
       document.execCommand('Unselect',false,0);
   }
  </script>
 </head>
 <body>
  <div align="center">
  input values : ---- <br>
  <textarea align="left" id="txtArea" name="txtArea" style="width:300px;height:50px"></textarea>
  <br>
  <input type="button" id="btnCopy" name="btnCopy" value=" Copy " onClick="doCopy()" />
  <input type="button" id="btnCut" name="btnCut" value=" Cut " onClick="doCut()" />
  <input type="button" id="btnSelectAll" name="btnSelectAll" value=" Select All " onClick="doSelectAll()" />
  <input type="button" id="btnPaste" name="btnPaste" value=" Paste " onClick="doPaste()" />
  <input type="button" id="btnDelete" name="btnDelete" value=" Delete " onClick="doDelete()" />
  <input type="button" id="btnUndo" name="btnUndo" value=" Undo " onClick="doUndo()" />
  <input type="button" id="btnUnselect" name="btnUnselect" value=" Undo " onClick="doUnselect()" />
  <br>
  Manipulate <br>
  <textarea align="left" id="txtArea1" onpaste="alert('yes');" name="txtArea1" style="width:300px;height:70px" ></textarea>
  </div>
 </body>
</html>





这些东西用于IE9.

谢谢

Arvind





This stuff is for IE9.

Thanks

Arvind

推荐答案

尝试此代码用于IE ...

try this this code is for IE...

 <script language="javascript">
    function doCopy()
   {
        document.execCommand('Copy',false,0);
   }
   function doPaste()
   {
        document.execCommand('Paste');
   }
   function doCut()
   {
        document.execCommand('Cut',false,0);
   }
   function doSelectAll()
   {
        document.execCommand('SelectAll',false,0);
    }
  function doDelete()
   {
        document.execCommand('Delete',false,0);
   }
    function doUndo()
    {
        document.execCommand('Undo',false,0);
    }
    function doUnselect()
    {
        document.execCommand('Unselect',false,0);
    }
  </script>
 </head>
<body>
<div align="center">
  input values : ---- <br>
 <textarea align="left" id="txtArea" name="txtArea" style="width:300px;         height:50px"></textarea>
 <br>
  <input type="button" id="btnCopy" name="btnCopy" value=" Copy "  önclick="doCopy()" />
  <input type="button" id="btnCut" name="btnCut" value=" Cut "  önclick="doCut()" />
  <input type="button" id="btnSelectAll" name="btnSelectAll" value=" Select All "  önclick="doSelectAll()" />
  <input type="button" id="btnPaste" name="btnPaste" value=" Paste "  önclick="doPaste()" />
  <input type="button" id="btnDelete" name="btnDelete" value=" Delete "  önclick="doDelete()" />
 <input type="button" id="btnUndo" name="btnUndo" value=" Undo "  önclick="doUndo()" />
 <input type="button" id="btnUnselect" name="btnUnselect" value=" Undo "  önclick="doUnselect()" />
  <br>
  Manipulate <br>
  <textarea align="left" id="txtArea1"  önpaste="alert('yes');" name="txtArea1" style="width:300px;height:70px" ></textarea>
   </div>


这篇关于如何使用js粘贴文本行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆