TWebBrowser 如何在 IE8 上将插入符位置设置为 INPUT(text) 字段的末尾

发布于 2024-12-29 01:38:01 字数 726 浏览 2 评论 0原文

我有一个通过 TWebBrowser 加载网页的应用程序,在此页面上我有一些 HTML 输入。我想要的是更改输入的值并将插入符位置设置到末尾。

这就是我现在所拥有的:

procedure SetInputValue(Document : IHTMLDocument2; const ElementId, NewValue : String);

var Doc   : IHTMLDocument3;
    El    : IHTMLElement;

begin
 Doc := Document as IHTMLDocument3;
 if Assigned(Doc) then
  begin
   El := Doc.getElementById(ElementId);
   if Assigned(El) then
    begin
     if El.tagName = 'INPUT' then
      (El as IHTMLInputElement).Value := NewValue;
      (El as IHTMLInputElement).select;
    end;
  end;
end;

这段代码设置输入值并突出显示文本部分。 我知道 IHTMLInputTextElement2 接口但仅在 IE9 下可用

I have an application that loads a webpage via TWebBrowser and on this page I have some HTML inputs. What I want is to change the value of an input and set the caret position to the end.

This is what I have at the moment:

procedure SetInputValue(Document : IHTMLDocument2; const ElementId, NewValue : String);

var Doc   : IHTMLDocument3;
    El    : IHTMLElement;

begin
 Doc := Document as IHTMLDocument3;
 if Assigned(Doc) then
  begin
   El := Doc.getElementById(ElementId);
   if Assigned(El) then
    begin
     if El.tagName = 'INPUT' then
      (El as IHTMLInputElement).Value := NewValue;
      (El as IHTMLInputElement).select;
    end;
  end;
end;

This piece of code sets the input value and highlights the text portion.
I am aware of the IHTMLInputTextElement2 Interface but it is only available from IE9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2025-01-05 01:38:01

您应该使用 IHTMLTxtRange

var Tr: IHTMLTxtRange;

Tr := (El as IHTMLInputElement).createTextRange;
Tr.collapse(true);
Tr.moveEnd('character', Length(NewValue));
Tr.moveStart('character', Length(NewValue));
Tr.select();   

You should use IHTMLTxtRange

var Tr: IHTMLTxtRange;

Tr := (El as IHTMLInputElement).createTextRange;
Tr.collapse(true);
Tr.moveEnd('character', Length(NewValue));
Tr.moveStart('character', Length(NewValue));
Tr.select();   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文