试用过IHTMLElement.focus不行,在这之前加入
   if WebBrowser1.Document <> nil then 
    with WebBrowser1.Application as IOleobject do 
     DoVerb(OLEIVERB_UIACTIVATE, nil, WebBrowser1, 0, Handle, GetClientRect); 
这段代码先让窗口获得焦点的,也不行,怎么回事儿呢?

解决方案 »

  1.   

    procedure   Focus(Document:IHTMLDocument2); 
    var   IDisp:IDispatch; 
            pElement:IHTMLElement; 
            i:integer; 
    begin 
            for   I   :=   0   to   Document.all.length   -   1   do         //   遍历所有HTML元素 
            begin 
                    {得到当前的IDispatch接口} 
                    IDisp:=Document.all.item(i,i); 
                    IDisp.QueryInterface(IID_IHTMLElement,pElement); 
                    if   CompareText(pElement.tagName, 'INPUT ')=0   then 
                    begin 
                            Document.parentWindow.execScript(Format( 'document.all(%d).focus(); ',[i]), 'JScript '); 
                            {执行JavaScript,将焦点移动到该元素上} 
                            exit; 
                    end; 
            end;         //   for 
    end; :)
      

  2.   

    兄弟回得真是快,太感谢了。
    有个疑问,这里的执行脚本,和IHTMLElement2.focus有什么区别呢?
      

  3.   

    Document.parentWindow.execScript('document.getElementById(''id'').focus','javascript');