我想将SpanElement插入到DOM的当前(光标)位置。 我使用这个VB6函数:
Private Function InsertSpan(Tipo As String, Nome As String)
Dim oSpan As MSHTML.HTMLSpanElement
Dim oCurElement As MSHTML.IHTMLElement
Dim oDOM As MSHTML.HTMLDocument
Set oDOM = HTMLEdit.DOM
Set oCurElement = oDOM.activeElement
Set oSpan = oDOM.createElement("SPAN")
oSpan.Id = Tipo & "." & Nome
oSpan.className = Tipo
oSpan.innerHTML = "<b><font size='3' color='#000000'></font></b>"
oSpan.lastChild.lastChild.innerText = "[" & Nome & "]"
oSpan.Style.backgroundColor = "yellow"
oSpan.hideFocus = True
oSpan.contentEditable = "false"
oSpan.Style.verticalAlign = "middle"
oCurElement.appendChild oSpan
oDOM.parentWindow.Focus
End Function
但是SpanElement会附加到当前元素。 谢谢,路易吉。
答案 0 :(得分:1)
我解决了它取代
oCurElement.appendChild oSpan
与
Dim objRange As IHTMLTxtRange
Set objRange = oDOM.selection.createRange
objRange.pasteHTML oSpan.outerHTML
感谢所有人!
答案 1 :(得分:0)
如果我正确理解您的问题,您可以尝试更换:
oCurElement.appendChild oSpan
使用:
oCurElement.insertAdjacentHTML“beforeBegin”,oSpan.outerHTML