<tfoot id='9yEbj'></tfoot>

  • <small id='9yEbj'></small><noframes id='9yEbj'>

    <i id='9yEbj'><tr id='9yEbj'><dt id='9yEbj'><q id='9yEbj'><span id='9yEbj'><b id='9yEbj'><form id='9yEbj'><ins id='9yEbj'></ins><ul id='9yEbj'></ul><sub id='9yEbj'></sub></form><legend id='9yEbj'></legend><bdo id='9yEbj'><pre id='9yEbj'><center id='9yEbj'></center></pre></bdo></b><th id='9yEbj'></th></span></q></dt></tr></i><div id='9yEbj'><tfoot id='9yEbj'></tfoot><dl id='9yEbj'><fieldset id='9yEbj'></fieldset></dl></div>
    <legend id='9yEbj'><style id='9yEbj'><dir id='9yEbj'><q id='9yEbj'></q></dir></style></legend>
        • <bdo id='9yEbj'></bdo><ul id='9yEbj'></ul>

        Javascript 将密钥代码发送到 &lt;textarea&gt;元

        时间:2023-09-04
      1. <small id='tJnQ6'></small><noframes id='tJnQ6'>

            • <legend id='tJnQ6'><style id='tJnQ6'><dir id='tJnQ6'><q id='tJnQ6'></q></dir></style></legend>
              <i id='tJnQ6'><tr id='tJnQ6'><dt id='tJnQ6'><q id='tJnQ6'><span id='tJnQ6'><b id='tJnQ6'><form id='tJnQ6'><ins id='tJnQ6'></ins><ul id='tJnQ6'></ul><sub id='tJnQ6'></sub></form><legend id='tJnQ6'></legend><bdo id='tJnQ6'><pre id='tJnQ6'><center id='tJnQ6'></center></pre></bdo></b><th id='tJnQ6'></th></span></q></dt></tr></i><div id='tJnQ6'><tfoot id='tJnQ6'></tfoot><dl id='tJnQ6'><fieldset id='tJnQ6'></fieldset></dl></div>

                <tbody id='tJnQ6'></tbody>
                <tfoot id='tJnQ6'></tfoot>
                • <bdo id='tJnQ6'></bdo><ul id='tJnQ6'></ul>
                  本文介绍了Javascript 将密钥代码发送到 &lt;textarea&gt;元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我不知道如何在 textarea 元素上触发 keydown 事件,例如想象一下我有两个 textarea 元素,当我在第一个中输入内容时,我希望第二个框也显示输入,但由于某种原因,我必须通过事件来完成.这是我尝试过的,但它不起作用:

                  I can't figure out how to trigger a keydown event on a textarea element, e.g. imagine i have two textarea elements and when i type something in the first one, I want the second box to show the typing as well, but for a certain reason I have to do it via events. This is what I tried and it doesn't work:

                  <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
                  
                  <textarea id='first'></textarea>
                  <textarea id='second'></textarea>
                  
                  <script>
                      jQuery("#first").keydown(function(event)
                      {
                          var keydown = jQuery.Event("keydown")
                          keydown.keyCode = event.keyCode
                          keydown.which = event.which
                          jQuery("#second").trigger(keydown)
                      })
                  </script>
                  

                  有什么想法可以实现吗?

                  Any ideas how could I achieve that?

                  推荐答案

                  事件被发送,浏览器只是没有反应(即在文本区域中放置字符)

                  The event is sent, the browser just doesn't react to it (i.e., put characters in the textarea)

                  并非所有浏览器(据我所知)都允许您调度事件,而不仅仅是触发它们.但即使这样做也远非完美

                  Not all browsers (that I know of) allow you to dispatch events, not just trigger them. But even doing that is far from perfect

                  // Gecko only
                  $("#first").keypress(function(event)
                  {
                    var evt = document.createEvent('KeyEvents');
                    evt.initKeyEvent(
                        event.type
                      , event.bubbles
                      , event.cancelable
                      , event.view
                      , event.ctrlKey
                      , event.altKey
                      , event.shiftKey
                      , event.metaKey
                      , event.keycode
                      , event.charCode
                    );
                    $('#second')[0].dispatchEvent( evt );
                  });
                  

                  试试这个例子,你就会明白我所说的它远非完美的意思.基本上,做你所要求的方式也是你说你做不到的方式 - 按价值.

                  Try this example and you'll see what I mean by how it's far from perfect. Basically, the way to do what you're asking is also the way you say you can't - by value.

                  但是,您可以将自定义数据与事件一起传递,从而得到如下所示的解决方案

                  However, you can pass custom data along with the event, which leads to a solution that looks like this

                  $("#first").bind( 'keyup change', function(event)
                  {
                    $('#second').trigger( 'mimic', $(this).val() );
                  });
                  $("#second").bind( 'mimic', function( event, value )
                  {
                    $(this).val( value );
                  })
                  

                  这样就够了吗?

                  这篇关于Javascript 将密钥代码发送到 &lt;textarea&gt;元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:鼠标悬停圆圈 HTML5 画布 下一篇:jQuery live('click') 触发右键单击

                  相关文章

                  最新文章

                  <small id='4GgQx'></small><noframes id='4GgQx'>

                  • <bdo id='4GgQx'></bdo><ul id='4GgQx'></ul>

                    <legend id='4GgQx'><style id='4GgQx'><dir id='4GgQx'><q id='4GgQx'></q></dir></style></legend>
                  1. <i id='4GgQx'><tr id='4GgQx'><dt id='4GgQx'><q id='4GgQx'><span id='4GgQx'><b id='4GgQx'><form id='4GgQx'><ins id='4GgQx'></ins><ul id='4GgQx'></ul><sub id='4GgQx'></sub></form><legend id='4GgQx'></legend><bdo id='4GgQx'><pre id='4GgQx'><center id='4GgQx'></center></pre></bdo></b><th id='4GgQx'></th></span></q></dt></tr></i><div id='4GgQx'><tfoot id='4GgQx'></tfoot><dl id='4GgQx'><fieldset id='4GgQx'></fieldset></dl></div>

                    <tfoot id='4GgQx'></tfoot>