这个问题与:在 TWebBrowser 中加载字符串(HTML 代码)的最佳方法是什么? https://stackoverflow.com/questions/39773033/which-is-the-best-way-to-load-a-string-html-code-in-twebbrowser
我尝试使用 doc.body.style.fontFamily 更改 TWebBrowser 中的字体,但没有任何反应。字体仍然是TimesNewRoman。
procedure THTMLEdit.SetHtmlCode(CONST HTMLCode: string);
VAR
Doc: Variant;
begin
if NOT Assigned(wbBrowser.Document)
then wbBrowser.Navigate('about:blank');
WHILE wbBrowser.ReadyState < READYSTATE_INTERACTIVE
DO Application.ProcessMessages;
Doc := wbBrowser.Document;
Doc.Clear;
Doc.Write(HTMLCode);
doc.body.style.fontFamily:='Arial'; <------ won't work
Doc.DesignMode := 'On';
Doc.Close;
end;
关闭文档后,您需要让文档再次具有交互性。
例如。:
procedure TForm1.SetHtmlCode(CONST HTMLCode: string);
VAR
Doc: Variant;
begin
if NOT Assigned(wbBrowser.Document)
then wbBrowser.Navigate('about:blank');
//WHILE wbBrowser.ReadyState < READYSTATE_INTERACTIVE // not really needed
//DO Application.ProcessMessages;
Doc := wbBrowser.Document;
//Doc.Clear; // not needed
Doc.Write(HTMLCode);
Doc.Close;
Doc.DesignMode := 'On';
WHILE wbBrowser.ReadyState < READYSTATE_INTERACTIVE
DO Application.ProcessMessages;
doc.body.style.fontFamily:='Arial';
ShowMessage(doc.body.outerHTML); // test it
end;
但我认为最好的办法是处理OnDocumentComplete
您知道您有一个有效的文档/正文,并设置样式或其他需要的内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)