在这个教程里,我们将解释Dojo的事件标准化和dojo/keys以及如何使用dojo/keys来处理键盘事件。
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.9.2/dojo/dojo.js" data-dojo-config="async: true"> </script> <body> <h1>Press any key</h1> keyCode value: <input type="text" id="keyCode" size="2"> </body>
require(["dojo/on", "dojo/domReady!"], function(on) { on(document, "keyup", function(event) { document.getElementById("keyCode").value = event.keyCode; }); });
<body> <h1>Press Up or Down Arrow Keys</h1> <input type="text" id="input1" value="up"> <input type="text" id="input2" value="down"> <input type="submit" id="send" value="send"> </body>
require(["dojo/dom-construct", "dojo/on", "dojo/query", "dojo/keys", "dojo/domReady!"], function(domConstruct, on, query, keys) { query("input[type='text']").on("keydown", function(event) { //query returns a nodelist, which has an on() function available that will listen //to all keydown events for each node in the list switch(event.keyCode) { case keys.UP_ARROW: event.preventDefault(); //preventing the default behavior in case your browser // uses autosuggest when you hit the down or up arrow. log("up arrow has been pressed"); break; case keys.DOWN_ARROW: event.preventDefault(); //preventing the default behavior in case your browser // uses autosuggest when you hit the down or up arrow. log("down arrow has been pressed"); break; case keys.ENTER: log("enter has been pressed"); break; default: log("some other key: " + event.keyCode); } }); });
<body> <h1>Press Up/Down Arrow Or Enter Keys to traverse form.</h1> <h2>Home/End will go to the beginning or end.</h2> <form id="traverseForm"> First Name: <input type="text" id="firstName"> Last Name: <input type="text" id="lastName"> Email Address: <input type="text" id="email"> Phone Number: <input type="text" id="phone"> <input type="submit" id="send" value="send"> </form> </body>
require(["dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/query", "dojo/keys", "dojo/NodeList-traverse", "dojo/domReady!"], function(dom, domConstruct, on, query, keys) { var inputs = query("input"); on(dom.byId("traverseForm"), "keydown", function(event) { var node = query.NodeList([event.target]); var nextNode; //on listens for the keydown events inside of the div node, on all form elements switch(event.keyCode) { case keys.UP_ARROW: nextNode = node.prev("input"); if(nextNode[0]){ //if not first element nextNode[0].focus(); //moving the focus from the current element to the previous } break; case keys.DOWN_ARROW: nextNode = node.next("input"); if(nextNode[0]){ //if not last element nextNode[0].focus(); //moving the focus from the current element to the next } break; case keys.HOME: inputs[0].focus(); break; case keys.END: inputs[inputs.length - 2].focus(); break; case keys.ENTER: event.preventDefault(); //prevent default keeps the form from submitting when the enter button is pressed //on the submit button if(event.target.type !== "submit"){ nextNode = node.next("input"); if(nextNode[0]){ //if not last element nextNode[0].focus(); //moving the focus from the current element to the next } }else { // submit the form log("form submitted!"); } break; default: log("some other key: " + event.keyCode); } }); });