为什么IE 8使光标跳到这个JS的textarea的结尾? [英] Why does IE 8 make the cursor jump to the end of the textarea for this JS?

查看:184
本文介绍了为什么IE 8使光标跳到这个JS的textarea的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://jsfiddle.net/tYXTX/

在Firefox中,使用上述脚本(在下面包含内容),您可以通过点击字符串中间并输入,或使用键盘后退键(和ctrl +向左箭头键)来编辑textarea的内容)。

In Firefox, with the above script (included inline below), you can edit the textarea's contents at any point either by clicking in the middle of the string and typing, or using the keyboard back keys (and ctrl+left arrow).

在IE中,光标总是跳到结束。为什么会这样,我该如何防止呢?

In IE, the cursor always jumps to the end. Why is this, and how can I prevent it?

HTML:

<textarea id="bob" name="bob">Some textarea content</textarea>
<div id="debug"></div>

JS:

$(document).ready(function(){
    $("#bob").keyup(function(){
        $("#bob").val($("#bob").val().substring(0,160));
        $("#debug").append("\n+");
    }); 
});


推荐答案

substring()每次,只有当文本长度大于160时:

Instead of truncating $("#bob") using substring() every time, do it only when the text length is greater than 160:

$(document).ready(function(){
    var oldtext = $("#bob").val();

    $("#bob").keyup(function(){
        if( $("#bob").val().length > 160 )
            $("#bob").val(oldtext);
        else
            oldtext = $("#bob").val();

        $("#debug").append("\n+");
    });
});

在IE中,每当< textarea> 得到修改,光标将跳到结束。

In IE, whenever the <textarea> gets modified, the cursor will jump to the end.

这篇关于为什么IE 8使光标跳到这个JS的textarea的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆