0
我需要获取与图像关联的链接。 正如你可以在下面的代码中看到的,我有TWebBrowser组件的自定义。我拦截鼠标点击WebBrowser视图。TWebBrowser:当我点击链接图像时获取链接href
当前的代码适用于常见链接,但对于图像的情况下,当我单击图像时无法获取href链接。
当我在图片的点击,我得到的链接HREF =“https://vimeo.com/194387045”
任何其他方式我能去吗?
<p> </p>
<p><a href="https://vimeo.com/194387045" target="_blank">
<img src="http://172.16.0.16/static/comunica/436dde8236d078cff2dc76deaa113dbb"
alt="" /></a></p>
方法:
Procedure TJBWebBrowser.ValidateLinkClick;
Var
LElement: IHTMLElement;
LLink, LTag: String;
LCancel: Boolean;
LDocument: IHTMLDocument2;
Begin
LDocument := IHTMLDocument2(Document);
If Not Assigned(LDocument) Then
Exit;
LCancel := False;
LElement := LDocument.parentWindow.event.srcElement;
LTag := Trim(LowerCase(LElemento.tagName));
If LTag = 'a' Then
LLink := Trim(LElement.getAttribute('href', 0));
If Assigned(FOnURLClick) Then
FOnURLClick(Self, LLink, LCancel);
If (LLink <> EmptyStr) And (Not LCancel) Then
ShellExecute(0, Nil, PChar(LLink), Nil, Nil, SW_SHOWNORMAL);
End;
所以,你说,'srcElement'是实际的''本身,而不是''的'包含内部'?我希望''标签是'srcElement'。真正的问题是,为什么你首先要处理这样的HTML事件,而不是使用浏览器自己的“OnBeforeNavigate2”事件呢?它为您提供正在导航的网址,以及取消导航的选项。 –
@RemyLebeau,我已经实现了一个在浏览器中捕获点击链接的过程。 OnBeforeNavegate2事件提供导航中的URL,例如WebBrowser1.Navegate('https://www.google.com')。在这种情况下,如果调用了Navegate方法,则会执行OnBeforeNavegate2。这不是我的情况。我有一个HTML,我设置为浏览器,我需要验证用户点击的链接。在我的示例中,当我点击图片时,返回的URL是http://172.16.0.16/static/comunica/436dde8236d078cff2dc76deaa113dbb。但是我需要返回https://vimeo.com/194387045 – Delphiman
@Delphiman,不。每次你点击一个链接并导航到一个URL时,会触发'OnBeforeNavigate2'。请参阅:http://stackoverflow.com/a/25311587/937125 – kobik