用getElementById试试 WebBrowser1.OleObject.document.all.getElementById('file1').focus(); 记着是这样,要不就是这样 uses mshtml; (WebBrowser1.OleObject.document as IHtmldocument3).getElementById('file1').focus();
uses MSHTML, SysUtils, Variants;function GetElementById(const Doc: IDispatch; const Id: string): IDispatch; var Document: IHTMLDocument2; // IHTMLDocument2 interface of Doc Body: IHTMLElement2; // document body element Tags: IHTMLElementCollection; // all tags in document body Tag: IHTMLElement; // a tag in document body I: Integer; // loops thru tags in document body begin Result := nil; // Check for valid document: require IHTMLDocument2 interface to it if not Supports(Doc, IHTMLDocument2, Document) then raise Exception.Create('Invalid HTML document'); // Check for valid body element: require IHTMLElement2 interface to it if not Supports(Document.body, IHTMLElement2, Body) then raise Exception.Create('Can''t find <body> element'); // Get all tags in body element ('*' => any tag name) Tags := Body.getElementsByTagName('*'); // Scan through all tags in body for I := 0 to Pred(Tags.length) do begin // Get reference to a tag Tag := Tags.item(I, EmptyParam) as IHTMLElement; // Check tag's id and return it if id matches if AnsiSameText(Tag.id, Id) then begin Result := Tag; Break; end; end; end;
使用: procedure TForm1.Button1Click(Sender: TObject); var Elem: IHTMLElement; begin Elem := GetElementById(WebBrowser1.Document, 'myid') as IHTMLElement; if Assigned(Elem) then ShowMessage( 'Tag name = <' + Elem.tagName + '>'#10 + 'Tag id = ' + Elem.id + #10 + 'Tag innerHTML = "' + Elem.innerHTML + '"' ); end;
----<input type="file" name="file1" size=100 >-----
WebBrowser1.OleObject.document.all.nameditem('file1').focus();ie6上通过,ie7上不行.
WebBrowser1.OleObject.document.all.getElementById('file1').focus();
记着是这样,要不就是这样
uses mshtml;
(WebBrowser1.OleObject.document as IHtmldocument3).getElementById('file1').focus();
MSHTML, SysUtils, Variants;function GetElementById(const Doc: IDispatch; const Id: string): IDispatch;
var
Document: IHTMLDocument2; // IHTMLDocument2 interface of Doc
Body: IHTMLElement2; // document body element
Tags: IHTMLElementCollection; // all tags in document body
Tag: IHTMLElement; // a tag in document body
I: Integer; // loops thru tags in document body
begin
Result := nil;
// Check for valid document: require IHTMLDocument2 interface to it
if not Supports(Doc, IHTMLDocument2, Document) then
raise Exception.Create('Invalid HTML document');
// Check for valid body element: require IHTMLElement2 interface to it
if not Supports(Document.body, IHTMLElement2, Body) then
raise Exception.Create('Can''t find <body> element');
// Get all tags in body element ('*' => any tag name)
Tags := Body.getElementsByTagName('*');
// Scan through all tags in body
for I := 0 to Pred(Tags.length) do
begin
// Get reference to a tag
Tag := Tags.item(I, EmptyParam) as IHTMLElement;
// Check tag's id and return it if id matches
if AnsiSameText(Tag.id, Id) then
begin
Result := Tag;
Break;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Elem: IHTMLElement;
begin
Elem := GetElementById(WebBrowser1.Document, 'myid') as IHTMLElement;
if Assigned(Elem) then
ShowMessage(
'Tag name = <' + Elem.tagName + '>'#10 +
'Tag id = ' + Elem.id + #10 +
'Tag innerHTML = "' + Elem.innerHTML + '"'
);
end;