[英]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: "<myImage>" (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 = "<" + Document.GetElementById("image1").GetAttribute("src") + ">";
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 = "<" + image.GetAttribute("src") + ">";
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.