在js中重新启用对键盘的访问

时间:2014-01-24 15:12:55

标签: javascript jquery textarea

我试图在我的textarea(#texttype)中完全禁用鼠标,但是当你单击包装器(#line)时,它会将光标移动到textarea中写入的内容的末尾。下面的代码几乎可以工作但是一旦取消选择textarea,单击包装器不会将光标放回到最后,就像文本区域被锁定所以用户不能再输入...

HTML:

<div id ="line">
  <textarea wrap="off" draggable="false" id="texttype" name="texttype" spellcheck="false" class="type" onkeyup="countChar(this)" onkeypress="keypressed(e)"></textarea>
</div>

JS:

var toggle_mouse = $('#line').mousedown(function(event){
    $('#texttype').focus(function(){
        this.selectionStart = this.selectionEnd = this.value.length;
    });
});

var mouse_off = $('#texttype').mousedown(function(event){
    event.preventDefault();
});

2 个答案:

答案 0 :(得分:0)

试试这段代码:

var mouse_off = $('#texttype').mousedown(function (event) {
    event.preventDefault();
})
.focus(function () {
    this.selectionStart = this.selectionEnd = this.value.length;
});

var toggle_mouse = $('#line').mousedown(function (event) {
    mouse_off.focus();
});

演示:http://jsfiddle.net/P3Bkp/

你的错误是你试图在mousedown事件处理程序中绑定textarea焦点事件。你不应该这样做。

答案 1 :(得分:0)

不确定这会起作用 - 但可能使文字无法选择而不是禁用鼠标?

#textarea a {
 -webkit-user-select:none; 
 -moz-user-select:none; 
 -ms-user-select:none; 
 user-select:none;
}