如下所示:
<script type="text/JavaScript"> function jsCopy(){ var e=document.getElementById("contents");//對象是contents e.select(); //選擇對象 document.execCommand("Copy"); //執(zhí)行瀏覽器復制命令 } </script> <textarea id="contents" cols="40" rows="5"></textarea><br /><input type="button" onClick="jsCopy();" value="復制" />js復制無非是以下三步:
1、獲取復制的對象:document.getElementById("contents")這句話就是獲取ID=“contents”的textarea。
2、全選對象的內容:e.select()
3、用execCommand("")函數(shù)將內容復制。
下面來詳細介紹execCommand("")函數(shù)的參數(shù):
1、格式:document.execCommand(sCommand[,交互方式, 動態(tài)參數(shù)])
2、參數(shù)詳解:
1、〖全選〗命令的實現(xiàn)
[格式]:document.execCommand(”selectAll”)
[說明]將選種網(wǎng)頁中的全部內容!
2、〖打開〗命令的實現(xiàn)
[格式]:document.execCommand(”open”)
[說明]這跟VB等編程設計中的webbrowser控件中的命令有些相似,大家也可依此琢磨琢磨。
3、〖另存為〗命令的實現(xiàn)
[格式]:document.execCommand(”saveAs”)
[說明]將該網(wǎng)頁保存到本地盤的其它目錄!
4、〖打印〗命令的實現(xiàn)
[格式]:document.execCommand(”print”)
[說明]當然,你必須裝了打印機!
Js代碼下面列出的是指令參數(shù)及意義
//相當于單擊文件中的打開按鈕document.execCommand(”O(jiān)pen”); //將當前頁面另存為document.execCommand(”SaveAs”); //剪貼選中的文字到剪貼板;document.execCommand(”Cut”,”false”,null); //刪除選中的文字;document.execCommand(”Delete”,”false”,null); //改變選中區(qū)域的字體;document.execCommand(”FontName”,”false”,sFontName); //改變選中區(qū)域的字體大小;document.execCommand(”FontSize”,”false”,sSize|iSize); //設置前景顏色;document.execCommand(”ForeColor”,”false”,sColor); //使絕對定位的對象可直接拖動;document.execCommand(”2D-Position”,”false”,”true”); //使對象定位變成絕對定位;document.execCommand(”AbsolutePosition”,”false”,”true”); //設置背景顏色;document.execCommand(”BackColor”,”false”,sColor); //使選中區(qū)域的文字加粗;document.execCommand(”Bold”,”false”,null); //復制選中的文字到剪貼板;document.execCommand(”Copy”,”false”,null); //設置指定錨點為書簽;document.execCommand(”CreateBookmark”,”false”,sAnchorName); //將選中文本變成超連接,若第二個參數(shù)為true,會出現(xiàn)參數(shù)設置對話框;document.execCommand(”CreateLink”,”false”,sLinkURL); //設置當前塊的標簽名;document.execCommand(”FormatBlock”,”false”,sTagName); //相當于單擊文件中的打開按鈕document.execCommand(”O(jiān)pen”); //將當前頁面另存為document.execCommand(”SaveAs”); //剪貼選中的文字到剪貼板;document.execCommand(”Cut”,”false”,null); //刪除選中的文字;document.execCommand(”Delete”,”false”,null); //改變選中區(qū)域的字體;document.execCommand(”FontName”,”false”,sFontName); //改變選中區(qū)域的字體大小;document.execCommand(”FontSize”,”false”,sSize|iSize); //設置前景顏色;document.execCommand(”ForeColor”,”false”,sColor); //使絕對定位的對象可直接拖動;document.execCommand(”2D-Position”,”false”,”true”); //使對象定位變成絕對定位;document.execCommand(”AbsolutePosition”,”false”,”true”); //設置背景顏色;document.execCommand(”BackColor”,”false”,sColor); //使選中區(qū)域的文字加粗;document.execCommand(”Bold”,”false”,null); //復制選中的文字到剪貼板;document.execCommand(”Copy”,”false”,null); //設置指定錨點為書簽;document.execCommand(”CreateBookmark”,”false”,sAnchorName); //將選中文本變成超連接,若第二個參數(shù)為true,會出現(xiàn)參數(shù)設置對話框;document.execCommand(”CreateLink”,”false”,sLinkURL); //設置當前塊的標簽名;document.execCommand(”FormatBlock”,”false”,sTagName);
注:火狐不支持此方法!
以上就是小編為大家?guī)淼膉s復制內容到剪貼板代碼,js復制代碼的簡單實例全部內容了,希望大家多多支持武林網(wǎng)~
新聞熱點
疑難解答