使用 javascript 查找文本区域内的插入符位置 xy

发布于 2024-11-29 04:58:58 字数 1727 浏览 1 评论 0原文

我想做的是 - 我可能不知道名字 - 文本区域内的预测帮助输入器。它使用 jquery 自动完成。当用户在文本区域(id=test)中输入“[[g”时,会打开一个具有自动完成功能的输入(id=example),因此它会在“data”中搜索。当用户找到所需数据时,必须按 Shif+Enter 将数据插入到文本区域中,并以“]]”结束。

我怎样才能找到卡雷的位置以使输入出现在附近?

我不想找到卡雷的索引,而是像 xy 绝对位置之类的东西。

你有什么建议我?

上面的代码:

<textarea onkeydown="predicao(this);" cols="40" rows="10" id="test" onfocus="this.focus()"></textarea>
<input id="example" style="display: none;" onkeyup="insert(this, event);"/>

<script language="Javascript">
<!--

function predicao(objeto){
    comprimento = objeto.value.length;
    var antipenultimo = comprimento - 4;
    var input = objeto.value.substring(antipenultimo,comprimento);
    var output = "";
    for(i=0; i<input.length; ++i){
        if(output != "") output += ", ";
        output += input.charCodeAt(i);
    }
    if (output == "91, 91, 103, 32"){

        var preditor = document.getElementById('example');
        preditor.value = '';
        preditor.style.display = 'block';
        preditor.focus();
        preditor.select();
    }  
}
function insert(objeto, evt){
    var e = evt || event;
    var code = e.keyCode || e.which;
    if(e.shiftKey && code == '13') {
        var texto = document.getElementById('test').value;
        texto += objeto.value+']]';
        document.getElementById('test').focus();
        document.getElementById('test').value = texto;
        objeto.style.display = 'none';
    }
}

$(document).ready(function(){
    var data = "Afrikaans Català Deutsch English Esperanto Suomi Français Galego Hrvatski Magyar Bahasa Indonesia Italiano Basa Jawa".split(" ");
$("#example").autocomplete(data);});
</script>

What Im trying to do is - i dont know the name maybe - a Prediction Help Inputter inside a textarea. It uses jquery autocomplete. When the user types '[[g ' inside textarea (id=test), a input with autocomplete is opened (id=example), so it search in 'data'. When the user find the desired data, he must press Shif+Enter to insert the data into the textarea, closing with ']]'.

How could I find the position of the carret to make the input appears near there?

I dont want to find the index of the carret, but something like the x y absolute position.

What do you suggest me?

Code above:

<textarea onkeydown="predicao(this);" cols="40" rows="10" id="test" onfocus="this.focus()"></textarea>
<input id="example" style="display: none;" onkeyup="insert(this, event);"/>

<script language="Javascript">
<!--

function predicao(objeto){
    comprimento = objeto.value.length;
    var antipenultimo = comprimento - 4;
    var input = objeto.value.substring(antipenultimo,comprimento);
    var output = "";
    for(i=0; i<input.length; ++i){
        if(output != "") output += ", ";
        output += input.charCodeAt(i);
    }
    if (output == "91, 91, 103, 32"){

        var preditor = document.getElementById('example');
        preditor.value = '';
        preditor.style.display = 'block';
        preditor.focus();
        preditor.select();
    }  
}
function insert(objeto, evt){
    var e = evt || event;
    var code = e.keyCode || e.which;
    if(e.shiftKey && code == '13') {
        var texto = document.getElementById('test').value;
        texto += objeto.value+']]';
        document.getElementById('test').focus();
        document.getElementById('test').value = texto;
        objeto.style.display = 'none';
    }
}

$(document).ready(function(){
    var data = "Afrikaans Català Deutsch English Esperanto Suomi Français Galego Hrvatski Magyar Bahasa Indonesia Italiano Basa Jawa".split(" ");
$("#example").autocomplete(data);});
</script>

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

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

发布评论

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

评论(2

层林尽染 2024-12-06 04:58:58

类似的东西:

var pos = $('textarea').caret(); // using caret plugin...
var lines = $('textarea').val().slice(0, pos).replace(/\t/g, '    ').split('\n');
var y = lines.length;
var x = lines[y-1].length;

对于固定宽度字体应该工作得相当好。

Something like:

var pos = $('textarea').caret(); // using caret plugin...
var lines = $('textarea').val().slice(0, pos).replace(/\t/g, '    ').split('\n');
var y = lines.length;
var x = lines[y-1].length;

Should work reasonably well for fix-width fonts.

月野兔 2024-12-06 04:58:58

如果您只想通过 JS 来完成此操作:

function doGetCaretPosition (ctrl) {
    var CaretPos = 0;   // IE Support
    if (document.selection) {
    ctrl.focus ();
        var Sel = document.selection.createRange ();
        Sel.moveStart ('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    }
    // Firefox support
    else if (ctrl.selectionStart || ctrl.selectionStart == '0')
        CaretPos = ctrl.selectionStart;
    return (CaretPos);
}

礼貌: http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
演示:http://demo.vishalon.net/getset.htm

If you're looking to do it via JS only:

function doGetCaretPosition (ctrl) {
    var CaretPos = 0;   // IE Support
    if (document.selection) {
    ctrl.focus ();
        var Sel = document.selection.createRange ();
        Sel.moveStart ('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    }
    // Firefox support
    else if (ctrl.selectionStart || ctrl.selectionStart == '0')
        CaretPos = ctrl.selectionStart;
    return (CaretPos);
}

Courtesy: http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
Demo: http://demo.vishalon.net/getset.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文