我们都知道delphi可以接收WebBrower中的操作动作,比如单击某个链接,但是如何发过来呢?
 比如我想用delphi的一个按钮,单击后,让WebBrower里的某个onClick事件对应响应呢?
 或者也响应一下某个链接反应呢?    先谢过各位xdjm了,谢谢!

解决方案 »

  1.   

    var
      i:integer;
      t:OleVariant;
    begin
      t := WebBrowser1.Document;
      for i := 0 to t.all.length - 1 do
      begin
        if t.all.item(i).tagName = 'INPUT' then
        begin
          if t.all.item(i).type = 'submit' then
          begin
            t.all.item(i).click;
            exit;
          end;
        end;
      end;
    end;
      

  2.   

    oDoc := (Sender as TWebBrowser).Document;  for I := 0 to oDoc.All.Length - 1 do begin
        oItem := oDoc.All.Item(I);
        if oItem.tagName = 'A' then begin
          vAttri := oitem.getAttribute('protocol', 0); //获得链接属性
          if vAttri = 'http:' then
          begin
            vAttri := oitem.getAttribute('href', 0);
            if pos(('http://www.delphibbs.com', vAttri), vAttri) > 0 then //如果链接中包含地址
            begin
              oitem.click;
              break;
            end;
          end;
        end;
      end; 
      

  3.   

    窗体上放一个TwebBrowser控件uses MSHTM;
    procedure TForm1.Button1Click(Sender: TObject);var
      doc: IHTMLDocument2;
      all: IHTMLElementCollection;
      len, i, flag: integer;
      item: IHTMLElement;
      vAttri: Variant;
    begin    //获得Webbrowser对象中的文档对象
      doc := WebBrowser1.document as ihtmldocument2;
        //获得文档中所有的HTML元素集合
      all := doc.get_all;
      len := all.get_length;
        //访问HTML元素集合中的每一个元素
      for i := 0 to len - 1 do
      begin
        item := all.item(i, varempty) as IHTMLElement;
          //如果该元素是一个链接
        if item.Get_tagname = 'A' then
        begin
          flag := 0;
          vAttri := item.getAttribute('protocol', flag); //获得链接属性
          if vAttri = 'http:' then
          begin
            vAttri := item.getAttribute('href', flag);
            if pos('http://www.delphibbs.com', vAttri) > 0 then //如果链接中包含大富翁的地址
             WebBrowser1.Navigate(vAttri );//点击这个链接
          end;
        end;
      end;
    end; 
      

  4.   

    谢谢yq3woaini,
      其实我点击这个链接并不是要去连接网站,而是要WebBrowser1BeforeNavigate2传递一个参数。我的WebBrowser1上HTML代码是用<td align="right"><a href="L381">这是我要的显示内容</a></td>这样的形式。
      

  5.   

    procedure TForm1.Button2Click(Sender: TObject);
    var
      E,I:IHTMLElement;
      ID: string;
    begin
      ID := '你能知道的ID';
      //<img id="ID" src="\Images\thefile.gif" align="absbottom">
      //就是这里的ID
      //I:=
      I := (WebBrowser1.Document as IHTMLDocument2).images.item(ID,0) as IHTMLElement;
      //两个都试一下
      E := (WebBrowser1.Document as IHTMLDocument2).all.item(ID,0) as IHTMLElement;
      if Assigned(E) then
        E.onclick;
      if Assigned(I) then
        I.onclick;
    end;