1
我需要从我在Delphi XE10(VCL)的TWebBrowser组件中显示的网站中删除一个小图像。我花了几个小时的搜索,我尝试了很多代码,但它不能按我的需要工作。从TWebBrowser中的活动html中删除特定的IMG标记
这是我的代码片段:
procedure TForm16.WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; const [Ref] URL: OleVariant);
var
Doc: IHTMLDocument2;
ElementCollection: IHTMLElementCollection;
Frames: IHTMLElementCollection;
Element: IHTMLElement;
Frame: IHTMLDOMNode;
i: Integer;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
ElementCollection := Doc.body.all as IHTMLElementCollection;
Frames := ElementCollection.tags('IMG') as IHTMLElementCollection;
if Frames <> nil then
begin
for i := 0 to Frames.length - 1 do
begin
Element := Frames.item(i, 0) as IHTMLElement;
Frame := Element as IHTMLDOMNode;
if Frame <> nil then
begin
Frame.parentNode.removeChild(Frame);
end;
end;
end;
end;
不幸的是它会删除所有图像。我想删除具有特定HREF的特定图片。你能帮助我吗?
你将它们全部遍历并全部删除。为什么要这样做,如果你只想删除其中的一个。 –
感谢您的回复。 –
我不想循环它们,以便我可以删除具有href ='exp.com/exp.png'的img节点,但我不知道如何实现它 –