2016-12-07 157 views
0

我需要获取与图像关联的链接。 正如你可以在下面的代码中看到的,我有TWebBrowser组件的自定义。我拦截鼠标点击WebBrowser视图。TWebBrowser:当我点击链接图像时获取链接href

当前的代码适用于常见链接,但对于图像的情况下,当我单击图像时无法获取href链接。

当我在图片的点击,我得到的链接HREF =“https://vimeo.com/194387045”

任何其他方式我能去吗?

<p>&nbsp;</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; 

回答

0

我发现我的疑问解决方案。

感谢大家的帮助。

按照改变后的代码:

Procedure TJBWebBrowser.ValidateLinkClick; 
Var 
    LElement: IHTMLElement; 
    LLink, LTag: String; 
    LCancel: Boolean; 
    LDocument: IHTMLDocument2; 
Begin 

    LCancel := False; 
    LDocument := IHTMLDocument2(Document); 

    If Not Assigned(LDocument) Then 
    Exit; 

    //Old line 
    //LElement := LDocument.parentWindow.event.srcElement; 

    //New line 
    LElement := LDocument.activeElement; 
    if not Assigned(LElement) then 
    Exit; 

    LTag := Trim(LowerCase(LElement.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;