I. Description
Cancels the default action for the event.
This method notifies the Web browser not to perform the default action associated with the event (if such an action exists). For example, if the type attribute is "submit", any event handle can be called at any stage of event propagation, and by calling the method, you can prevent the form from being submitted. Note that if the Cancelable property of the Event object is Fasle, there is no default action, or the default action cannot be blocked. In either case, calling the method has no effect.
Second, the grammar
Event.preventdefault ()
Iii. Example 3.1 block <a> element jump
<a href= "Http://www.mazey.net/baby/blog" target= "_blank" id= "a-prevent" > click I see will not jump </a><script> document.getElementById (' a-prevent '). AddEventListener (' click ', Function (e) {var eObj = e | | window.event; Eobj.preventdefault ();}); </script>
Click I see will not jump
3.2 Disable <input> element keyboard input
<input type= "text" id= "input-prevent" placeholder= "try to enter"/><script>document.getelementbyid (' Input-prevent '). AddEventListener (' KeyPress ', function (e) {var eObj = e | | window.event; Eobj.preventdefault ();}); </script>
JavaScript disables input by Preventdefault () Input[type=text] but retains the cursor
This article from "Do not know not to ask" blog, please be sure to keep this source http://mazey.blog.51cto.com/12997993/1946864
JavaScript disables input by Preventdefault () Input[type=text] but retains the cursor