如何在MSHTML中设置整个HTML?

发布于 2024-08-26 02:46:34 字数 364 浏览 1 评论 0原文

如何在MSHTML中设置整个HTML?

我正在尝试使用此作业:

   (Document as IHTMLDocument3).documentElement.innerHTML := 'abc';  

但出现错误:

“目标元素对此无效 操作”

我也尝试过使用,

(Document as IHTMLDocument2).write 

但这种形式只将 HTML 添加到正文部分,我需要替换所有 HTML 源。
有人知道我该怎么做吗?

提前致谢。

How to set entire HTML in MSHTML?

I am trying using this assignment:

   (Document as IHTMLDocument3).documentElement.innerHTML := 'abc';  

but I got the error:

"Target element invalid for this
operation"

I've also tried using

(Document as IHTMLDocument2).write 

but this form only adds HTML into the body section, and I need to replace all the HTML source.
Does somebody have any idea of how I do this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…楫夜泊 2024-09-02 02:46:34

这是我的一些旧代码,看看它是否对你有帮助:

type
  THackMemoryStream = class(TMemoryStream);

procedure Clear(const Document: IHTMLDocument2);
begin
  Document.write(PSafeArray(VarArrayAsPSafeArray(VarArrayOf([WideString('')]))));
  Document.close;
end;

procedure LoadFromStream(const Document: IHTMLDocument2; Stream: TStream);
var
  Persist: IPersistStreamInit;
begin
  Clear(Document);
  Persist := (Document as IDispatch) as IPersistStreamInit;
  OleCheck(Persist.InitNew);
  OleCheck(Persist.Load(TStreamAdapter.Create(Stream)));
end;

procedure SetHtml(const Document: IHTMLDocument2; const Html: WideString);
var
  Stream: TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  try
    THackMemoryStream(Stream).SetPointer(PWideChar(Html), (Length(Html) + 1) * SizeOf(WideChar));
    Stream.Seek(0, soFromBeginning);
    LoadFromStream(Document, Stream);
  finally
    Stream.Free;
  end;
end;

Here's some of my old code, see if it helps you:

type
  THackMemoryStream = class(TMemoryStream);

procedure Clear(const Document: IHTMLDocument2);
begin
  Document.write(PSafeArray(VarArrayAsPSafeArray(VarArrayOf([WideString('')]))));
  Document.close;
end;

procedure LoadFromStream(const Document: IHTMLDocument2; Stream: TStream);
var
  Persist: IPersistStreamInit;
begin
  Clear(Document);
  Persist := (Document as IDispatch) as IPersistStreamInit;
  OleCheck(Persist.InitNew);
  OleCheck(Persist.Load(TStreamAdapter.Create(Stream)));
end;

procedure SetHtml(const Document: IHTMLDocument2; const Html: WideString);
var
  Stream: TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  try
    THackMemoryStream(Stream).SetPointer(PWideChar(Html), (Length(Html) + 1) * SizeOf(WideChar));
    Stream.Seek(0, soFromBeginning);
    LoadFromStream(Document, Stream);
  finally
    Stream.Free;
  end;
end;
梦巷 2024-09-02 02:46:34

作为替代方案,您还可以使用 TEmbededWB,它是 Web 浏览器的扩展包装器,并且具有一些功能提供此功能的易于使用的方法。

As an alternative you can also use TEmbededWB which is an extended wrapper around a web browser and has some easy to use methods that provide this functionality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文