简体   繁体   English

使用jQuery caret插件在IE中textarea中的插入符位置

[英]Caret position in textarea in IE using jQuery caret plugin

I'm using this plguin to get caret position of textarea. 我正在使用这只企鹅来获取文本区域的插入符位置。 I have a <div> , clicking on which shows the <ul> with smilie list to put one of them into the textarea. 我有一个<div> ,单击它会显示带有表情符号列表的<ul> ,将其中之一放入文本区域。 Clicking on <div> fires the blur event on textarea. 单击<div>会在textarea上触发blur事件。 I save the caret position on blur: 我将插入符号的位置保存为模糊:

oRoot.blur(function() {
    caret_pos = $(this).caret();
});

And after user click on smilie, I put it in the textarea where the caret was before it lost the focus: 在用户单击smilie之后,我将其放置在插入符号失去焦点之前的文本区域中:

oList
    .delegate('.chat-smile', 'click', function() {
        var oRoot = $(this).parent().data('oRoot');

        if (is_default_value(oRoot)) oRoot.val('');

        oRoot.val(oRoot.caret(caret_pos).caret().replace($(this).attr('smilie-code')));

        toggle_list($(this).parent());
    });

The problem is that in IE it seems that the plugin doesn't work if textarea has no focus, and in IE the blur event handler is fired after the focus is lost. 问题是,在IE中,如果textarea没有焦点,则插件似乎无法正常工作;而在IE中,焦点丢失后便触发了blur事件处理程序。

Any ideas of workaround this? 有什么解决方法的想法吗? I was thinking of saving the caret position on click , keyup , focus for IE.. 我正在考虑将插入符号的位置保存在IE的clickkeyupfocus上。

I've tried the workaround I thought of befor (saving the caret position on click, keyup, focus for IE). 我已经尝试过我想到的解决方法(保存IE的点击,键入,焦点时的插入符号位置)。 So now function which binds events to save caret position looks like this: 现在绑定事件以保存插入符号位置的函数如下所示:

function bind_save_caret_pos() {
    var event_to_bind = $.browser.msie ? 'keyup mouseup' : 'blur';

    oRoot.bind(event_to_bind, function() {
        caret_pos = $(this).caret();
    });
}

keyup deals with typing, deleting, moving around the text with arrows etc. keyup涉及键入,删除,带箭头的文本移动等。

mouseup deals with selecting peace of text, moving cursor etc. mouseup用于选择文本的和平度,移动光标等。

Here is another way to get the caret position in IE. 是获得IE中插入符号位置的另一种方法。 You can use it , if you like. 您可以根据需要使用它。

Hope it helps. 希望能帮助到你。

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

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