pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      1. <legend id='JD0Lm'><style id='JD0Lm'><dir id='JD0Lm'><q id='JD0Lm'></q></dir></style></legend>

        • <bdo id='JD0Lm'></bdo><ul id='JD0Lm'></ul>
        <tfoot id='JD0Lm'></tfoot>

        <small id='JD0Lm'></small><noframes id='JD0Lm'>

        <i id='JD0Lm'><tr id='JD0Lm'><dt id='JD0Lm'><q id='JD0Lm'><span id='JD0Lm'><b id='JD0Lm'><form id='JD0Lm'><ins id='JD0Lm'></ins><ul id='JD0Lm'></ul><sub id='JD0Lm'></sub></form><legend id='JD0Lm'></legend><bdo id='JD0Lm'><pre id='JD0Lm'><center id='JD0Lm'></center></pre></bdo></b><th id='JD0Lm'></th></span></q></dt></tr></i><div class="drlrbht" id='JD0Lm'><tfoot id='JD0Lm'></tfoot><dl id='JD0Lm'><fieldset id='JD0Lm'></fieldset></dl></div>

        在javascript中檢測按鍵的最簡單方法

        Simplest way to detect keypresses in javascript(在javascript中檢測按鍵的最簡單方法)
          <bdo id='2SPOt'></bdo><ul id='2SPOt'></ul>
              <tbody id='2SPOt'></tbody>

            <tfoot id='2SPOt'></tfoot>

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

                  <small id='2SPOt'></small><noframes id='2SPOt'>

                  本文介紹了在javascript中檢測按鍵的最簡單方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有一個 javascript 游戲的想法(我將使用 EaselJS 制作它),我必須檢測按鍵.在互聯網上環顧四周后,我看到了很多建議(使用 window.onkeypress,使用 jQuery 等),但幾乎每個選項都有反駁.你們有什么建議?使用 jQuery 聽起來很容易,但我幾乎沒有使用該庫的經驗(而且我也不是 javascript 的資深人士)所以我寧愿避免使用它.如果 jQuery 是最好的選擇,有人可以舉一個很好的例子(解釋會很棒)嗎?

                  I have an idea for a game in javascript (I'm going to make it with EaselJS) and I'll have to detect keypresses. After looking around on the internet I've seen a lot of suggestions (use window.onkeypress, use jQuery, etc.) but for almost every option there's a counterargument. What do you guys suggest? Using jQuery for this sounds easy enough but I have virtually no experience with that library (and I'm not particulary a veteran at javascript either) so I'd rather avoid it. If jQuery is the best option, can someone give a good example (with explanation would be awesome) of how to do this?

                  我想這被問了很多,但我找不到任何明確的答案.提前致謝!

                  I guess this gets asked a lot but I couldn't find any clear answers. Thanks in advance!

                  推薦答案

                  用純Javascript,最簡單的是:

                  With plain Javascript, the simplest is:

                  document.onkeypress = function (e) {
                      e = e || window.event;
                      // use e.keyCode
                  };
                  

                  但是有了這個,你只能為事件綁定一個處理程序.

                  But with this, you can only bind one handler for the event.

                  此外,您可以使用以下方法將多個處理程序潛在地綁定到同一事件:

                  In addition, you could use the following to be able to potentially bind multiple handlers to the same event:

                  addEvent(document, "keypress", function (e) {
                      e = e || window.event;
                      // use e.keyCode
                  });
                  
                  function addEvent(element, eventName, callback) {
                      if (element.addEventListener) {
                          element.addEventListener(eventName, callback, false);
                      } else if (element.attachEvent) {
                          element.attachEvent("on" + eventName, callback);
                      } else {
                          element["on" + eventName] = callback;
                      }
                  }
                  

                  在任何一種情況下,keyCode 在瀏覽器中都不一致,因此需要檢查和找出更多內容.注意 e = e ||window.event - 這是 Internet Explorer 的正常問題,將事件放在 window.event 中,而不是將其傳遞給回調.

                  In either case, keyCode isn't consistent across browsers, so there's more to check for and figure out. Notice the e = e || window.event - that's a normal problem with Internet Explorer, putting the event in window.event instead of passing it to the callback.

                  參考資料:

                  • https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/keypress
                  • https://developer.mozilla.org/en-US/docs/DOM/EventTarget.addEventListener

                  使用 jQuery:

                  $(document).on("keypress", function (e) {
                      // use e.which
                  });
                  

                  參考:

                  • http://api.jquery.com/on/

                  除了 jQuery 是一個大型"庫之外,jQuery 確實有助于解決瀏覽器之間的不一致問題,尤其是窗口事件……這是不可否認的.希望很明顯,我為您的示例提供的 jQuery 代碼更優雅、更短,但以一致的方式完成了您想要的工作.您應該能夠相信 e (事件)和 e.which (鍵碼,用于知道按下了哪個鍵)是準確的.在純 Javascript 中,除非您執行 jQuery 庫內部所做的所有事情,否則很難知道.

                  Other than jQuery being a "large" library, jQuery really helps with inconsistencies between browsers, especially with window events...and that can't be denied. Hopefully it's obvious that the jQuery code I provided for your example is much more elegant and shorter, yet accomplishes what you want in a consistent way. You should be able to trust that e (the event) and e.which (the key code, for knowing which key was pressed) are accurate. In plain Javascript, it's a little harder to know unless you do everything that the jQuery library internally does.

                  注意有一個 keydown 事件,它不同于 keypress.您可以在此處了解有關它們的更多信息:onKeyPress Vs.onKeyUp 和 onKeyDown

                  Note there is a keydown event, that is different than keypress. You can learn more about them here: onKeyPress Vs. onKeyUp and onKeyDown

                  至于建議使用什么,如果您準備學習該框架,我肯定會建議使用 jQuery.同時,我想說你應該學習 Javascript 的語法、方法、特性,以及如何與 DOM 交互.一旦你理解了它是如何工作的以及發生了什么,你應該更容易使用 jQuery.對我來說,jQuery 讓事情變得更加一致和簡潔.最后,它是 Javascript,并包裝了語言.

                  As for suggesting what to use, I would definitely suggest using jQuery if you're up for learning the framework. At the same time, I would say that you should learn Javascript's syntax, methods, features, and how to interact with the DOM. Once you understand how it works and what's happening, you should be more comfortable working with jQuery. To me, jQuery makes things more consistent and is more concise. In the end, it's Javascript, and wraps the language.

                  另一個非常有用的 jQuery 例子是 AJAX.瀏覽器與 AJAX 請求的處理方式不一致,因此 jQuery 抽象了這一點,因此您不必擔心.

                  Another example of jQuery being very useful is with AJAX. Browsers are inconsistent with how AJAX requests are handled, so jQuery abstracts that so you don't have to worry.

                  以下幾點可能有助于做出決定:

                  Here's something that might help decide:

                  • http://www.jscripters.com/jquery-disadvantages-and-advantages/

                  這篇關于在javascript中檢測按鍵的最簡單方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Use IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
                  anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 項目中不起作用)
                  Ionic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用異步數據更新 Observable)
                  Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
                  In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何創建使用 Ionic 組件的自定義指令?)
                  Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(將 ViewChild 用于動態元素 - Angular 2 amp;離子2)

                    <tbody id='rFdAt'></tbody>
                1. <i id='rFdAt'><tr id='rFdAt'><dt id='rFdAt'><q id='rFdAt'><span id='rFdAt'><b id='rFdAt'><form id='rFdAt'><ins id='rFdAt'></ins><ul id='rFdAt'></ul><sub id='rFdAt'></sub></form><legend id='rFdAt'></legend><bdo id='rFdAt'><pre id='rFdAt'><center id='rFdAt'></center></pre></bdo></b><th id='rFdAt'></th></span></q></dt></tr></i><div class="5bhjppj" id='rFdAt'><tfoot id='rFdAt'></tfoot><dl id='rFdAt'><fieldset id='rFdAt'></fieldset></dl></div>
                  • <bdo id='rFdAt'></bdo><ul id='rFdAt'></ul>
                      <legend id='rFdAt'><style id='rFdAt'><dir id='rFdAt'><q id='rFdAt'></q></dir></style></legend>

                        <small id='rFdAt'></small><noframes id='rFdAt'>

                            <tfoot id='rFdAt'></tfoot>
                            主站蜘蛛池模板: 丝印油墨_水性油墨_环保油墨油漆厂家_37国际化工 | 渣土车电机,太阳能跟踪器电机,蜗轮蜗杆减速电机厂家-淄博传强电机 | 斗式提升机_链式斗提机_带式斗提机厂家无锡市鸿诚输送机械有限公司 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 洁净化验室净化工程_成都实验室装修设计施工_四川华锐净化公司 | 撕碎机_轮胎破碎机_粉碎机_回收生产线厂家_东莞华达机械有限公司 | 短信营销平台_短信群发平台_106短信发送平台-河南路尚 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 生产加气砖设备厂家很多,杜甫机械加气砖设备价格公道 | 消防设施操作员考试报名时间,报名入口,报考条件 | 驾驶式洗地机/扫地机_全自动洗地机_工业洗地机_荣事达工厂官网 | 蔡司三坐标-影像测量机-3D扫描仪-蔡司显微镜-扫描电镜-工业CT-ZEISS授权代理商三本工业测量 | 中红外QCL激光器-其他连续-半导体连续激光器-筱晓光子 | 成都竞价托管_抖音代运营_网站建设_成都SEM外包-成都智网创联网络科技有限公司 | 无菌实验室规划装修设计-一体化实验室承包-北京洁净净化工程建设施工-北京航天科恩实验室装备工程技术有限公司 | 釜溪印象网络 - Powered by Discuz!| 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 江西自考网-江西自学考试网| 伸缩器_伸缩接头_传力接头-巩义市润达管道设备制造有限公司 | 沥青灌缝机_路面灌缝机_道路灌缝机_沥青灌缝机厂家_济宁萨奥机械有限公司 | 房屋质量检测-厂房抗震鉴定-玻璃幕墙检测-房屋安全鉴定机构 | 杭州公司变更法人-代理记账收费价格-公司注销代办_杭州福道财务管理咨询有限公司 | 脱硫搅拌器厂家-淄博友胜不锈钢搅拌器厂家 | 寮步纸箱厂_东莞纸箱厂 _东莞纸箱加工厂-东莞市寮步恒辉纸制品厂 | 高光谱相机-近红外高光谱相机厂家-高光谱成像仪-SINESPEC 赛斯拜克 | 玖容气动液压设备有限公司-气液增压缸_压力机_增压机_铆接机_增压器 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 通信天线厂家_室分八木天线_对数周期天线_天线加工厂_林创天线源头厂家 | 郑州墨香品牌设计公司|品牌全案VI设计公司 | 哈希余氯测定仪,分光光度计,ph在线监测仪,浊度测定仪,试剂-上海京灿精密机械有限公司 | 铝镁锰板_铝镁锰合金板_铝镁锰板厂家_铝镁锰金属屋面板_安徽建科 | 云南标线|昆明划线|道路标线|交通标线-就选云南云路施工公司-云南云路科技有限公司 | 游动电流仪-流通式浊度分析仪-杰普仪器(上海)有限公司 | 联系我们-腾龙公司上分客服微信19116098882 | 软装设计-提供软装装饰和软装配饰及软装陈设的软装设计公司 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 卫生型双针压力表-高温防腐差压表-安徽康泰电气有限公司 | 锡膏喷印机-全自动涂覆机厂家-全自动点胶机-视觉点胶机-深圳市博明智控科技有限公司 | 不锈钢复合板厂家_钛钢复合板批发_铜铝复合板供应-威海泓方金属复合材料股份有限公司 | 砂磨机_立式纳米砂磨机_实验室砂磨机-广州儒佳化工设备厂家 | 青岛空压机,青岛空压机维修/保养,青岛空压机销售/出租公司,青岛空压机厂家电话 |