我使用下面的代码:
const
HTML_DOC =
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">' +
'<BODY><P id="p1" style="width: 440px; height: 344px; margin: 3px 2px; float: left;">test</P></BODY>' +
'</HTML>';
procedure TForm1.Button1Click(Sender: TObject);
var
HTMLTxtRange: IHTMLTxtRange;
HTMLDocument: IHTMLDocument2;
HTMLElement: IHTMLElement;
HTMLStyle: IHTMLStyle;
begin
WebBrowser1.Navigate('about:blank');
while WebBrowser1.ReadyState < READYSTATE_COMPLETE do
Application.ProcessMessages;
HTMLDocument := WebBrowser1.Document as IHTMLDocument2;
HTMLTxtRange := (HTMLDocument.body as IHTMLBodyElement).createTextRange;
HTMLTxtRange.PasteHTML(HTML_DOC);
HTMLElement := (HTMLDocument as IHTMLDocument3).getElementById('p1');
if Assigned(HTMLElement) then
begin
HTMLStyle := HTMLElement.style {as IHTMLStyle2};
Memo1.Lines.Add(HTMLStyle.cssText); // MARGIN: 3px 2px; WIDTH: 440px; FLOAT: left; HEIGHT: 344px
Memo1.Lines.Add(HTMLStyle.getAttribute('margin', 0)); // 3px 2px
end;
end;
我的问题是:如何在不手动解析的情况下使用 DOM 集合获取可用样式属性的列表,HTMLStyle.cssText
即预期输出:
MARGIN
WIDTH
FLOAT
HEIGHT