createTextRange - IE8 中的奇怪行为

发布于 2024-08-07 12:24:02 字数 387 浏览 4 评论 0原文

我有以下问题。在作为自动完成器工作的文本输入字段中,它返回的一些建议比它更弱。当你离开球场时,问题就来了。在 IE 中,文本光标位于建议字符串的末尾,因此您实际上只能看到它的最后一部分。所以我使用下面的代码来修复这个问题,它在 IE6 下工作,但在 IE8 下不起作用,该字段始终被选中,我无法选择页面上的任何内容。

我的问题是,在我离开输入字段后,将光标移动到输入字段开头的正确方法是什么?

$('#myAutocompleter').blur(function(){
  textRange = this.createTextRange();
  textRange.collapse(true);
  textRange.select();
});

(所使用的代码是用 jQuery 编写的。)

I have the following problem. In text input filed which work as an auto-completer some of suggestions it returns are wither than it. The problem comes when you leave the field. In IE the text cursor is positioned on the end of the suggested string, so you can actually see only the last part of it. So I used the code bellow to fix this and it works under IE6, but in IE8 this doesn't work, the field is always selected and I can not select anything on the page.

My question is what is the right way to move the cursor in the beginning of input field, after I leave it?

$('#myAutocompleter').blur(function(){
  textRange = this.createTextRange();
  textRange.collapse(true);
  textRange.select();
});

(The used code is written in jQuery.)

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

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

发布评论

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

评论(3

澉约 2024-08-14 12:24:02

我相信您正在寻找的是文本范围的 .moveStart.moveEnd 方法:

$('#myAutocompleter').blur(function(){
  textRange = this.createTextRange();
  textRange.collapse(true);
  textRange.moveEnd('character',0);
  textRange.moveStart('character',0);
  textRange.select();
});

(在 IE8 中测试功能)

I believe what you're looking for are the .moveStart and .moveEnd methods of the text range:

$('#myAutocompleter').blur(function(){
  textRange = this.createTextRange();
  textRange.collapse(true);
  textRange.moveEnd('character',0);
  textRange.moveStart('character',0);
  textRange.select();
});

(Tested functional in IE8)

不必了 2024-08-14 12:24:02

我不确定我是否理解你的问题,但 IE 有自己的一套方法来处理页面上的文本选择,所以这就是它的行为不同的原因。

在这里查看教程:
http://www.quirksmode.org/dom/range_intro.html

这里是兼容性:
http://www.quirksmode.org/dom/w3c_range.html

如果不是您的问题,尝试在模糊内进行鼠标向上或单击事件检查并将选择代码放在那里?也许这会导致选择在将其放置在选择所在的位置之前从输入字段移开。

I'm not sure I understand your question, but IE has it's own set of methods for handling text selection on a page, so that would be why it behaves differently.

Look here for a tutorial:
http://www.quirksmode.org/dom/range_intro.html

And here for compatability:
http://www.quirksmode.org/dom/w3c_range.html

If that's not your problem, try doing a mouseup or click event check inside blur and putting the selection code there? Maybe that'll cause the selection to move away from the input field before placing it where the selection is.

鹤舞 2024-08-14 12:24:02

我也遇到过类似的情况,我想根据类似的事情来查看某些东西的顶部/底部。
我使用了 jQuery scrollTo 插件

).scrollTo('100%')

).scrollTo('0%')

编辑1:
我在这个领域使用它:

<textarea cols="57" rows="2" class="cssTextarea cptEntryArea"></textarea>

使用以下代码:

  $(".cptEntryArea").change(function()
        {
           $(this).scrollTo('0%');
   });

I had a similar situation where I wanted to see the top/bottom of something depending upon things like this.
I used the jQuery scrollTo plugin

).scrollTo('100%')

).scrollTo('0%')

EDIT 1:
I am using it on this field:

<textarea cols="57" rows="2" class="cssTextarea cptEntryArea"></textarea>

with this code:

  $(".cptEntryArea").change(function()
        {
           $(this).scrollTo('0%');
   });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文