如果没有有关您现实世界问题的更多详细信息,就很难提出好的/实用的建议。理论上,您可以通过使用 .inner/outerHTLM 和 .innerTEXT 等属性或调用 .create*、.remove* 或 .append* 等函数来操作 oHtmlFile 的文档树。
在代码中:
Dim oDOM : Set oDOM = CreateObject("HTMLFILE")
Dim sHTML : sHTML = "<html><head></head><body></body></html>"
WScript.Echo "Empty", TypeName(oDOM), "oDOM"
dumpDoc oDOM
oDOM.write sHTML
WScript.Echo "After .write", qq(sHTML)
dumpDoc oDOM
sHTML = "<p>G</p><p>H</p>"
oDOM.childNodes(0).childNodes(1).innerHTML = sHTML
WScript.Echo "After setting body's .innerHTML to", qq(sHTML)
dumpDoc oDOM
sHTML = "pipapo"
oDOM.childNodes(0).childNodes(1).childNodes(1).innerTEXT = sHTML
WScript.Echo "After changing second P's .innerTEXT to", qq(sHTML)
dumpDoc oDOM
oDOM.childNodes(0).childNodes(1).removeChild oDOM.childNodes(0).childNodes(1).childNodes(0)
WScript.Echo "After deleting first P"
dumpDoc oDOM
Dim oNode : Set oNode = oDOM.createElement("h1")
oNode.appendChild oDOM.createTextNode("tritratrulala")
oDOM.childNodes(0).childNodes(1).appendChild oNode
WScript.Echo "After using DOM to append an H1"
dumpDoc oDOM
sHTML = "<span>abracadabra</span>"
oDOM.childNodes(0).childNodes(1).childNodes(0).outerHTML = sHTML
WScript.Echo "After overwriting first P's outerHTML", qq(sHTML)
dumpDoc oDOM
Sub dumpDoc(oDOM)
If oDOM.documentElement Is Nothing Then
WScript.Echo "", "oDOM.documentElement Is Nothing"
WScript.Echo "", "oDOM.childNodes.length", oDOM.childNodes.length
Else
WScript.Echo "", "oDOM.documentElement:", oDOM.documentElement.tagName
WScript.Echo "", "oDOM.childNodes.length", oDOM.childNodes.length
dumpTree oDOM.documentElement, 2
End If
WScript.Echo "-----------------"
End Sub
Sub dumpTree(oNode, nL)
Select Case oNode.nodeType
Case 1
WScript.Echo Space(nL), oNode.nodeType, oNode.tagName, qq(Replace(oNode.innerHTML, vbCrLf, ""))
Dim oChild
For Each oChild In oNode.childNodes
dumpTree oChild, nL + 1
Next
Case 3
WScript.Echo Space(nL), oNode.nodeType, oNode.nodeName, qq(Replace(oNode.nodeValue, vbCrLf, ""))
End Select
End Sub
输出:
demoHtmlFile - demo HtmlFile
===============================================================================
Empty HTMLDocument oDOM
oDOM.documentElement Is Nothing
oDOM.childNodes.length 0
-----------------
After .write "<html><head></head><body></body></html>"
oDOM.documentElement: HTML
oDOM.childNodes.length 1
1 HTML "<HEAD></HEAD><BODY></BODY>"
1 HEAD ""
1 TITLE ""
1 BODY ""
-----------------
After setting body's .innerHTML to "<p>G</p><p>H</p>"
oDOM.documentElement: HTML
oDOM.childNodes.length 1
1 HTML "<HEAD></HEAD><BODY><P>G</P><P>H</P></BODY>"
1 HEAD ""
1 TITLE ""
1 BODY "<P>G</P><P>H</P>"
1 P "G"
3 #text "G"
1 P "H"
3 #text "H"
-----------------
After changing second P's .innerTEXT to "pipapo"
oDOM.documentElement: HTML
oDOM.childNodes.length 1
1 HTML "<HEAD></HEAD><BODY><P>G</P><P>pipapo</P></BODY>"
1 HEAD ""
1 TITLE ""
1 BODY "<P>G</P><P>pipapo</P>"
1 P "G"
3 #text "G"
1 P "pipapo"
3 #text "pipapo"
-----------------
After deleting first P
oDOM.documentElement: HTML
oDOM.childNodes.length 1
1 HTML "<HEAD></HEAD><BODY><P>pipapo</P></BODY>"
1 HEAD ""
1 TITLE ""
1 BODY "<P>pipapo</P>"
1 P "pipapo"
3 #text "pipapo"
-----------------
After using DOM to append an H1
oDOM.documentElement: HTML
oDOM.childNodes.length 1
1 HTML "<HEAD></HEAD><BODY><P>pipapo</P><H1>tritratrulala</H1></BODY>"
1 HEAD ""
1 TITLE ""
1 BODY "<P>pipapo</P><H1>tritratrulala</H1>"
1 P "pipapo"
3 #text "pipapo"
1 H1 "tritratrulala"
3 #text "tritratrulala"
-----------------
After overwriting first P's outerHTML "<span>abracadabra</span>"
oDOM.documentElement: HTML
oDOM.childNodes.length 1
1 HTML "<HEAD></HEAD><BODY><SPAN>abracadabra</SPAN><H1>tritratrulala</H1></BODY>"
1 HEAD ""
1 TITLE ""
1 BODY "<SPAN>abracadabra</SPAN><H1>tritratrulala</H1>"
1 SPAN "abracadabra"
3 #text "abracadabra"
1 H1 "tritratrulala"
3 #text "tritratrulala"
-----------------
(qq()、nodeType 常量,以及避免这些的策略
oDOM.childNodes(0).childNodes(1).removeChild oDOM.childNodes(0).childNodes(1).childNodes(0)
讨厌的东西留作练习)