2014-05-21 36 views
2

我在TWebbrowser中加载网页,但网页显示不正确,我无法更改编码。更改内联样式TWebbrower Delphi

如何在页面加载后在Delphi中更改内联样式?

这是我试图改变代码:我刚开始我以为我可以使用这种方法只是注入CSS样式表

<td width="200px" valign=top style="display:none; background-color:#576299; height:800px;"> 

<td width="200px" valign=top style="background-color:#576299; height:800px;"> 

这个CSS and TWebbrowser delphi但没有课程可以重写,我可以为所有表格设置样式,但我想在其中显示其他表格。

是否有无论如何搜索我正在寻找的HTML并将其替换为其他东西?

+0

页面在IE中加载时是否正常工作? – whosrdaddy

回答

1

如果网页未预期呈现,则表示webbrowser控件不使用与MSIE使用的相同呈现策略。为了解决这个问题,你可能需要使用FEATURE_BROWSER_EMULATIONsee this post

如果你想改变DOM,有几种方法可以存档:

路1:使用魔法oleobject。 页面加载成功后,您可以拨打WebBrowser1.oleobject.getElementById('foo').style := 'new_style';

方式2:使用MSHTML_TLB.pas中的接口。演示代码如下。

var 
    D2Ptr: IHTMLDocument2; 
    ElemPtr: IHTMLElement; 
begin 
    if Supports(WebBrowser1.Document, IHTMLDocument2, D2Ptr) then 
    begin 
    ElemPtr := D2Ptr.getElementById('foo'); // If the td has an id, you can use this method. 
    if ElemPtr <> nil then 
     try 
     ElemPtr.style := ElemPtr.style + '; display:none'; 
     except 
     end; 
    end; 
end; 

请注意,如果您正在查找的元素没有ID。你可能首先找到它的父母,他有一个ID,然后通过它的子元素。