简体   繁体   English

如何按空格键,然后在文本区域中按Backspace键:使用JQuery?

[英]How to press space key and then backspace key in a textarea: using JQuery?

Do we have any way to press any keyboard key in textarea using JQuery? 我们是否可以使用JQuery在textarea中按下任何键盘键?

Usage in My Case There are many textareas have height of 100, set by default onload (some issue with JQuery Autosize used by some developer of which i have no access to change and i am not authorized to change). 在我的案例中的用法有很多textareas的高度为100,默认情况下为onload设置(某些开发人员使用的JQuery Autosize存在一些问题,我无法更改,并且无权更改)。 There is some text displayed in textarea. 在文本区域中显示了一些文本。 If it is less say 10 character only, the height of textarea will Remain same. 如果少说10个字符,则文本区域的高度将保持不变。

Now when end user click in Textarea and then press any button to edit, then at sudden textarea gets adjusted to normal height. 现在,当最终用户单击Textarea并按任意按钮进行编辑时,突然文本区域将调整为正常高度。

So i want jquery to fire key-press(space then backspace) on load on all textareas. 所以我想让jquery在所有textarea上加载时触发key-press(space然后backspace)。 Do we have any way to press any keyboard key in textarea using JQuery? 我们是否可以使用JQuery在textarea中按下任何键盘键?

You can use the trigger event. 您可以使用触发事件。 See my jsfiddle 见我的jsfiddle

$("textarea").each(function(){
    var d = $.Event('keydown');
    var e = $.Event('keydown');
    d.which = 32; // space
    e.which = 8; // backspace
    this.focus();
    this.trigger(d);
    this.trigger(e);
}

Manually trigger resize: 手动触发调整大小:

$('textarea').each(function(){
    var evt = document.createEvent('Event');
    evt.initEvent('autosize:update', true, false);
    $(this).dispatchEvent(evt);
});

Destroying autoresize: 销毁自动调整大小:

$('textarea').each(function(){
    var evt = document.createEvent('Event');
    evt.initEvent('autosize:destroy', true, false);
    $(this).dispatchEvent(evt);
});

As unable to trigger click so used following code to increase and decrease textarea: 由于无法触发点击,因此使用以下代码来增加和减少文本区域:

autoHeight: function(e) {
    $(e).css({'height':'auto','overflow-y':'hidden'}).height(e.scrollHeight);

}, },

setTimeout(function(){
        $('textarea').each(function () {
            kontroll.autoHeight(this);
        }).on('input', function () {
            kontroll.autoHeight(this);
        });
    }, 200);

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

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