栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

同时将文本从一个文本区域写入另一个文本区域

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

同时将文本从一个文本区域写入另一个文本区域

绑定到

keyup
事件时,您会注意到延迟。通常绑定到
keydown
事件时,文本区域的值尚未更改,因此,在确定
keydown
事件期间按下的键之前,您无法更新第二个文本区域的值。对我们来说幸运的是,我们可以使用
String.fromCharCode()
将新按下的键附加到第二个文本区域。这样做是为了使第二文本区域快速更新而没有任何滞后:

$('.one').on('keydown', function(event){    var key = String.fromCharCode(event.which);    if (!event.shiftKey) {        key = key.toLowerCase();    }    $('.two').val( $(this).val() + key );});​

这是一个演示:http :
//jsfiddle.net/agz9Y/2/

这将使第二个文本区域具有与第一个文本区域相同的内容,如果您想将第一个文本区域附加到第二个文本区域,则只需将第一个文本区域的值添加到第二个文本区域即可,而不是覆盖:

$('.one').on('keydown', function(event){    var key = String.fromCharCode(event.which);    if (!event.shiftKey) {        key = key.toLowerCase();    }    $('.two').val( $('.two').val() + $(this).val() + key );});​

这是一个演示:http :
//jsfiddle.net/agz9Y/3/

更新资料

您可以对此进行一些更改,以便

.two
元素记住其自己的值:

$('.one').on('keydown', function(event){    var key = String.fromCharCode(event.which);    if (!event.shiftKey) {        key = key.toLowerCase();    }    //notice the value for the second textarea starts with it's data attribute    $('.two').val( $('.two').data('val') + ' -- ' + $(this).val() + key );});//set the `data-val` attribute for the second textarea$('.two').data('val', '').on('focus', function () {    //when this textarea is focused, return its value to the remembered data-attribute    this.value = $(this).data('val');}).on('change', function () {    //when this textarea's value is changed, set it's data-attribute to save the new value    //and update the textarea with the value of the first one    $(this).data('val', this.value);    this.value = this.value + ' -- ' + $('.one').val();});​


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号