简体   繁体   English

如何将整个HTMLElement的文本替换为其他内容?

[英]How to replace whole HTMLElement's text to something else?

Here's the example. 这是例子。

webBrowser1.Document.Body.InnerHtml contains: webBrowser1.Document.Body.InnerHtml包含:

<img id="image1" src="myImage.gif">

and in my MyWebBrowser class I want to replate whole img tag to some text, for example to string: "&ltmyImage&gt" (I need it for my chat application, if user doesn't want to see images) 在MyWebBrowser类中,我想将整个img标签重新映射为某些文本,例如字符串:“&ltmyImage&gt”(如果用户不想看到图像,我的聊天应用程序需要它)

I thought I could do something like that: 我以为我可以做这样的事情:

Document.GetElementById("image1").InnerHtml = "&lt" + Document.GetElementById("image1").GetAttribute("src") + "&gt";

but it throws exception. 但它引发异常。

Actually I resolved that by searching those specific tags in whole document and replacing it using String class' methods, but code doesn't look good. 实际上,我通过在整个文档中搜索这些特定标签并使用String类的方法替换了它们来解决了该问题,但是代码看起来并不好。 If there's more effective way to do that, do not bother answering my question. 如果有更有效的方法可以做到,请不要回答我的问题。

You can use the OuterHtml property: 您可以使用OuterHtml属性:

HtmlElement image = Document.GetElementById("image1");
image.OuterHtml = "&lt;" + image.GetAttribute("src") + "&gt;";

Note, however, that assigning to OuterHtml will invalidate the reference to the element, so image will not refer to the new content afterwards. 但是请注意,分配给OuterHtml将使对该元素的引用无效,因此image将不再引用新内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择特定的ComboBoxItem时,如何将可编辑的ComboBox的文本更改为其他内容? - How to change the editable ComboBox's text to something else when a specific ComboBoxItem is selected? 用其他东西替换字符串的一部分 - Replace part of string with something else 如何用其他东西替换字符串中的字符? - How can I replace a character in a string with something else? 如何将char(10)[sql server]替换为c#中的其他内容 - How to replace char(10) [sql server] to something else in c# 将重复的char替换为字符串中的其他内容 - Replace repeated char with something else in a string 如何在不覆盖整个文件的情况下替换文本文件中的单词 - how to replace a word in a text file without overwriting the whole file 如何替换整个文本中两个字符之间的字符串? - How to replace string between two characters in whole text? 您可以在HtmlDocument中用HtmlElement替换HtmlElement吗? - Can you replace an HtmlElement with an HtmlElement in an HtmlDocument? HTMLElement的GetAttribute(“ html”)或GetAttribute(“ text”)不返回值 - HTMLElement's GetAttribute(“html”) or GetAttribute(“text”) not returning a value 如何设置元素的HtmlElement类型? - How do I set the element's type of an HtmlElement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM