繁体   English   中英

在Android浏览器中将文本重点放在文本后面

[英]Focus textarea with caret after text in Android browser

我目前正在编写一个简单的webapp来查看Android浏览器中的推文。 我正在使用此代码将焦点集中在当前文本之后:

var oldContent = document.tweetBox.tweet.value;
document.tweetBox.tweet.value = '';
document.tweetBox.tweet.focus();
document.tweetBox.tweet.value = oldContent + to;

此代码在Chrome,Fluid,Opera,Firefox和Safari中完美运行。
最奇怪的部分是,如果我使用我的硬件键盘,光标开始在'到'文本后开始闪烁,但我输入的文本开始于我在执行JS之前输入的位置。
如果我使用软键盘,文本输入将从textarea的开头开始。

ps javascript是关注者建议的一部分,所以如果你开始输入@gn,它会在单独的div中建议@gnur_nl,当你按回车时,这个条目被选中。

更新:此行为似乎是浏览器错误的结果, 已提交错误报告

听起来像浏览器错误。 尝试手动设置插入位置:

var textArea = document.tweetBox.tweet, oldContent = textArea.value;
textArea.value = oldContent + to;
textArea.focus();
textArea.selectionStart = textArea.selectionEnd = textArea.value.length;
setCaret = function(obj,pos) {
    // IE Support
    if (document.selection) { 

        // Set focus on the element
        obj.focus ();

        // Create empty selection range
        var oSel = document.selection.createRange ();

        // Move selection start and end to 0 position
        oSel.moveStart ('character', -obj.value.length);

        // Move selection start and end to desired position
        oSel.moveStart ('character', pos);
    }

    //standard browsers
    else if (obj.selectionStart || obj.selectionStart == '0') {
        obj.selectionStart = pos;
        obj.selectionEnd = pos;
        obj.focus ();
    }
};

并设置到正确的位置

setCaret(document.getElementById("area"),document.getElementById("area").value.length);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM