//執行js方法 IHTMLWindow2 win = oDocument2.parentWindow; win.execScript("functiona();", "JavaScript"); //加入自定義js IHTMLDOMNode domNode = (IHTMLDOMNode)oLeftDocument2?.body; IHTMLElement script = oDocument2.createElement("script"); script.setAttribute("type", "text/javascript"); script.setAttribute("text", "function confirm(str) { return true; } function alert(str) { return true; }"); domNode.appendChild((IHTMLDOMNode)script); //以下代碼為遍歷各個節點的方法 private static string getHtmlDisplayContent(string html) { string cont = ""; mshtml.HTMLDocumentClass oc = new mshtml.HTMLDocumentClass(); mshtml.IHTMLDocument2 doc2 = oc; doc2.write(html); mshtml.IHTMLDocument3 HTMLDocument = (mshtml.IHTMLDocument3)doc2; traverseNodes(HTMLDocument.documentElement, ref cont); doc2.close(); return cont; } private static void traverseNodes(mshtml.IHTMLElement parentNode,ref string cont) { if (parentNode.innerText!=null) cont += parentNode.innerText; mshtml.IHTMLElementCollection nodes = (IHTMLElementCollection)parentNode.children; IEnumerator ienum= nodes.GetEnumerator(); while (ienum.MoveNext()) { IHTMLElement node = (IHTMLElement)ienum.Current; traverseNodes(node,ref cont); } }
document.execCommand()方法處理Html數據時常用語法格式 如下: 復制內容到剪貼板 代碼 : document.execCommand(sCommand[,交互方式, 動態參數 ]) 其中:sCommand為指令參數(如下例中的"2D-Position"),交互方式參數如果是true的話將顯示對話框,如果為false的話,則不顯示對話框(下例中的"false"即表示不顯示對話框),動態參數一般為一可用值或屬性 值(如下例中的"true")。 document.execCommand("2D-Position","false","true"); 調用execCommand()可以實現瀏覽器菜單 的很多功能. 如保存文件 ,打開新文件,撤消、重做操作...等等. 有了這個方法,就可以很容易的實現網頁中的文本編輯器. 如果靈活運用,可以很好的輔助我們完成各種項目. 使用的例子如下: 1、〖全選〗命令的實現 [格式]:document.execCommand("selectAll") [說明]將選種網頁中的全部內容! [舉例]在<body></body>之間加入: <a href="#" onclick=document.execCommand("selectAll")>全選</a> 2、〖打開〗命令的實現 [格式]:document.execCommand("open") [說明]這跟VB等編程設計中的web browser控件中的命令有些相似,大家也可依此琢磨琢磨。 [舉例]在<body></body>之間加入: <a href="#" onclick=document.execCommand("open")>打開</a> 3、〖另存為〗命令的實現 [格式]:document.execCommand("saveAs") [說明]將該網頁保存到本地盤的其它目錄! [舉例]在<body></body>之間加入: <a href="#" onclick=document.execCommand("saveAs")>另存為</a> 4、〖打印〗命令的實現 [格式]:document.execCommand("print") [說明]當然,你必須裝了打印機! [舉例]在<body></body>之間加入: <a href="#" onclick=document.execCommand("print")>打印</a> Js代碼 下面列出的是指令參數及意義 //相當於單擊文件中的打開按鈕 document.execCommand("Open"); //將當前頁面 另存為 document.execCommand("SaveAs"); //剪貼選中的文字到剪貼板; document.execCommand("Cut","false",null); //刪除選中的文字; document.execCommand("Delete","false",null); //改變選中區域的字體; document.execCommand("FontName","false",sFontName); //改變選中區域的字體大小; 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); //使選中區域的文字加粗; document.execCommand("Bold","false",null); //復制選中的文字到剪貼板; document.execCommand("Copy","false",null); //設置指定錨點為書簽; document.execCommand("CreateBookmark","false",sAnchorName); //將選中文 本變成超連接,若第二個參數為true,會出現參數設置對話框; document.execCommand("CreateLink","false",sLinkURL); //設置當前塊的標簽名; document.execCommand("FormatBlock","false",sTagName); //相當於單擊文件中的打開按鈕 document.execCommand("Open"); //將當前頁面另存為 document.execCommand("SaveAs"); //剪貼選中的文字到剪貼板; document.execCommand("Cut","false",null); //刪除選中的文字; document.execCommand("Delete","false",null); //改變選中區域的字體; document.execCommand("FontName","false",sFontName); //改變選中區域的字體大小; 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); //使選中區域的文字加粗; document.execCommand("Bold","false",null); //復制選中的文字到剪貼板; document.execCommand("Copy","false",null); //設置指定錨點為書簽; document.execCommand("CreateBookmark","false",sAnchorName); //將選中文本變成超連接,若第二個參數為true,會出現參數設置對話框; document.execCommand("CreateLink","false",sLinkURL); //設置當前塊的標簽名; document.execCommand("FormatBlock","false",sTagName); document對象execCommand通常在IE中在線處理Html數據時非常有用,它可以讓你輕而易舉實現文字的加粗、加顏色、加字體等一系列的命令。 D-Position 允許通過拖曳移動絕對定位的對象。 AbsolutePosition 設定元素的 position 屬性為“absolute”(絕對)。 BackColor 設置或獲取當前選中區的背景顏色。 BlockDirLTR 目前尚未支持。 BlockDirRTL 目前尚未支持。 Bold 切換當前選中區的粗體顯示與否。 BrowseMode 目前尚未支持。 Copy 將當前選中區復制到剪貼板。 CreateBookmark 創建一個書簽錨或獲取當前選中區或插入點的書簽錨的名稱。 CreateLink 在當前選中區上插入超級鏈接,或顯示一個對話框允許用戶指定要為當前選中區插入的超級鏈接的 URL。 Cut 將當前選中區復制到剪貼板並刪除之。 Delete 刪除當前選中區。 DirLTR 目前尚未支持。 DirRTL 目前尚未支持。 EditMode 目前尚未支持。 FontName 設置或獲取當前選中區的字體。 FontSize 設置或獲取當前選中區的字體大小。 ForeColor 設置或獲取當前選中區的前景(文本)顏色。 FormatBlock 設置當前塊格式化標簽。 Indent 增加選中文本的縮進。 InlineDirLTR 目前尚未支持。 InlineDirRTL 目前尚未支持。 InsertButton 用按鈕控件覆蓋當前選中區。 InsertFieldset 用方框覆蓋當前選中區。 InsertHorizontalRule 用水平線覆蓋當前選中區。 InsertIFrame 用內嵌框架覆蓋當前選中區。 InsertImage 用圖像覆蓋當前選中區。 InsertInputButton 用按鈕控件覆蓋當前選中區。 InsertInputCheckbox 用復選框控件覆蓋當前選中區。 InsertInputFileUpload 用文件上載控件覆蓋當前選中區。 InsertInputHidden 插入隱藏控件覆蓋當前選中區。 InsertInputImage 用圖像控件覆蓋當前選中區。 InsertInputPassword 用密碼控件覆蓋當前選中區。 InsertInputRadio 用單選鈕控件覆蓋當前選中區。 InsertInputReset 用重置控件覆蓋當前選中區。 InsertInputSubmit 用提交控件覆蓋當前選中區。 InsertInputText 用文本控件覆蓋當前選中區。 InsertMarquee 用空字幕覆蓋當前選中區。 InsertOrderedList 切換當前選中區是編號列表還是常規格式化塊。 InsertParagraph 用換行覆蓋當前選中區。 InsertSelectDropdown 用下拉框控件覆蓋當前選中區。 InsertSelectListbox 用列表框控件覆蓋當前選中區。 InsertTextArea 用多行文本輸入控件覆蓋當前選中區。 InsertUnorderedList 切換當前選中區是項目符號列表還是常規格式化塊。 Italic 切換當前選中區斜體顯示與否。 JustifyCenter 將當前選中區在所在格式化塊置中。 JustifyFull 目前尚未支持。 JustifyLeft 將當前選中區所在格式化塊左對齊。 JustifyNone 目前尚未支持。 JustifyRight 將當前選中區所在格式化塊右對齊。 LiveResize 迫使 MSHTML 編輯器在縮放或移動過程中持續更新元素外觀,而不是只在移動或縮放完成后更新。 MultipleSelection 允許當用戶按住 Shift 或 Ctrl 鍵時一次選中多於一個站點可選元素。 Open 目前尚未支持。 Outdent 減少選中區所在格式化塊的縮進。 OverWrite 切換文本狀態的插入和覆蓋。 Paste 用剪貼板內容覆蓋當前選中區。 PlayImage 目前尚未支持。 Print 打開打印對話框以便用戶可以打印當前頁。 Redo 目前尚未支持。 Refresh 刷新當前文檔。 RemoveFormat 從當前選中區中刪除格式化標簽。 RemoveParaFormat 目前尚未支持。 SaveAs 將當前 Web 頁面保存為文件。 SelectAll 選中整個文檔。 SizeToControl 目前尚未支持。 SizeToControlHeight 目前尚未支持。 SizeToControlWidth 目前尚未支持。 Stop 目前尚未支持。 StopImage 目前尚未支持。 StrikeThrough 目前尚未支持。 Subscript 目前尚未支持。 Superscript 目前尚未支持。 UnBookmark 從當前選中區中刪除全部書簽。 Underline 切換當前選中區的下划線顯示與否。 Undo 目前尚未支持。 Unlink 從當前選中區中刪除全部超級鏈接。 Unselect 清除當前選中區的選中狀態。 關於document.execCommand: 要執行編輯命令,可調用 document.execCommand,並傳遞對應於命令 ID 的字符串。另外還有可選的第二個參數 ,該參數指定如果可以應用的話是否顯示此命令的用戶界面。傳遞整數 1 將顯示用戶界面,整數 0 將跳過它。這個參數通常不用於編輯命令。因為默認值為 0,所以假如您沒有使用第三個參數(在這種情況下,還必須為第二個參數傳遞值),一般可以不管它。第三個參數也是可選的, 在可應用的情況下,使用它來將任何所需參數傳遞給該命令。