如何让TWEBBROWSE中加载的HTML页面中的一个文本框得到焦点,小弟菜鸟真的很急,哪位支个招吧。

解决方案 »

  1.   

    使文本框或是某个对象得到焦点用
    setFocus;如使EDIT得到焦点:edit1.SetFocus;
      

  2.   

    html页面里得到焦点应该是在html里控制的吧,
    读出html的源程序,添加相应代码。
    试试看。
      

  3.   

    我这里给一个执行按钮的Click事件的例子,我认为你稍微修改之即可达到要求。
    //执行指定名称的Object的Click事件
    procedure TFmRequest.ClickObject(const ObjName: string);
    var
      ObjHtml : IHtmlElement;
      CurDocument: IHTMLDocument2;//执行ExecuteTrans后取得的浏览器窗口接口
    begin
      IsFinished := False;
      ObjHtml := HtmlObjs.Item(ObjName, 0) as IHTMLElement;
      if ObjHtml = nil then Exit;
      
      ObjHtml.Click;//试试.Focus?用delphi的代码提示可以看到很多方法
      while True do
      begin
        Application.ProcessMessages;
        if IsFinished then Break;
      end;  WebBrowser.Document.QueryInterface(IHTMLDocument2, CurDocument);
      HtmlObjs := CurDocument.All;
    end;
      

  4.   

    在TFmRequest的Private区还有一句:
        HtmlObjs: IHtmlElementCollection;
    我在另一个地方对HtmlObjs赋值:
    //运行应用服务器上的交易
    procedure TFmRequest.ExecuteTrans(const TransUrl: string; const AutoLogin: Boolean);
    var
      ErrorStr: string;
      CurDocument: IHTMLDocument2;//执行ExecuteTrans后取得的浏览器窗口接口
    begin
      ..............
      WebBrowser.Document.QueryInterface(IHTMLDocument2, CurDocument);
      HtmlObjs := CurDocument.All;
      .....
    end;
      

  5.   

    这应该在html中控制吧!!!没试过
      

  6.   

    可惜的是ObjHtml : IHtmlElement
    IHTMLELEMENT没有SETFOCUS或FOCUS的属性或方法啊,我看过了,不信你试试哦
    有人能解决吗,谢谢先。
      

  7.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Doc: IHTMLDocument2;
      Form : IHTMLFormElement;
      Elements: IHTMLElementCollection;
      InputElem : IHTMLInputElement;
      i : Integer;
    begin
      WebBrowser1.Navigate('http://oakhome.xicp.net/search.htm');
      while(WebBrowser1.Busy)do
      begin
        Application.ProcessMessages;
      end;  Doc := WebBrowser1.Document as IHTMLDocument2;
      Elements := Doc.Forms as IHTMLElementCollection;
      Form := Elements.Item(0,varEmpty) as IHTMLFormElement;
      Elements := (Doc.All as IHTMLElementCollection).tags('input') as IHTMLElementCollection;
      for i := 0 to Elements.length-1 do
      begin
        InputElem := Elements.Item(i, varEmpty) as IHTMLInputElement;    if UpperCase(Trim(InputElem.name)) = 'KEYS' then
          InputElem.select;
      end;
    end;
      

  8.   

    var  form : IHTMLFormElement  ;
         d:IHTMLDocument2  ;
    begin
      with WebBrowser1 do begin
         d := document as IHTMLDocument2;
          (form.item(0,0) as IHTMLElement2).focus;
      end;end;
      

  9.   

    上面的代码将焦点放在http://oakhome.xicp.net/search.htm这个页面的name为keys的文本框中。记得在代码中引用MSHTML。