我使用 .NET WebBrowser 控件作为所见即所得的 html 编辑器。现在我想用链接替换 htmlelement 中的特殊字符串。我从这段代码中使用
foreach (var el in nodes)
{
string innertxt = el.InnerText;
MatchCollection matches = Regex.Matches(innertxt, pattern);
foreach (Match match in matches)
{
string finalLink = FinalWordLink(word_current, linkTemp);
int indexWord = match.Index;
int lenghWord = match.Length;
el.InnerText = el.InnerText.Remove(indexWord, lenghWord);
el.InnerText = el.InnerText.Insert(indexWord, finalLink);
}
}
但是我现在这不起作用并将新链接显示为文本我有问题,我不想替换innerhtml,因为这不好,并且可能替换错误的字符串,如标签、类等我知道如何在 htmlelement 中附加子项像这样 :
HtmlElement elem = HTMLEditor.Document.CreateElement("a");
elem.SetAttribute("href", "www.google.com");
elem.InnerText = "Click here";
el.AppendChild(elem);
但是我怎样才能在 find index 处追加子项?