0

我想将 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 附加到当前元素。谢谢,路易吉。

4

2 回答 2

1

我解决了它替换

oCurElement.appendChild oSpan

Dim objRange As IHTMLTxtRange
Set objRange = oDOM.selection.createRange
objRange.pasteHTML oSpan.outerHTML

谢谢大家!

于 2012-09-25T13:48:26.693 回答
0

如果我正确理解您的问题,您可以尝试替换:

oCurElement.appendChild oSpan

和:

oCurElement.insertAdjacentHTML "beforeBegin", oSpan.outerHTML

于 2012-09-12T15:41:59.617 回答