1. 程式人生 > >使用mshtml為頁面中的超級連結新增Click事件

使用mshtml為頁面中的超級連結新增Click事件

導讀:



在使用mshtml操作HTML頁面時可能遇到要為頁面的超級連結新增Click事件的操作,可以用下面的程式碼來完成:


None.gifforeach(mshtml.IHTMLElement2 eleLink inthis.doc.all)
ExpandedBlockStart.gifContractedBlock.gif
{
InBlock.gif    
if (eleLink.GetType().ToString() =="mshtml.HTMLAnchorElementClass")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
InBlock.gif        ((mshtml.HTMLAnchorEvents2_Event)eleLink).onclick 
+=new mshtml.HTMLAnchorEvents2_onclickEventHandler(this
.AnchorClick);
InBlock.gif        ((mshtml.HTMLAnchorElementClass)eleLink).href 
="#";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
privatebool AnchorClick(mshtml.IHTMLEventObj pEvtObj)
ExpandedBlockStart.gifContractedBlock.gif
{
InBlock.gif    
//新增單擊超級連結的處理邏輯
InBlock.gif    
//可以使用((mshtml.HTMLAnchorElementClass)(((mshtml.IHTMLElement)(((mshtml.IHTMLElement)(pEvtObj.srcElement)).parentElement))))獲得被單擊的超級連結並進一步獲得其href等相關屬性

ExpandedBlockEnd.gif
}


Life is like a boat, and I'm at sea.



本文轉自
http://www.cnblogs.com/waxdoll/archive/2005/10/22/259857.html