How to get the body tag
$("body") .keydown (function () {
if (event.keyCode == "13") {//keyCode=13
document.getElementById("btnSumit").click();
}
});
the $("body") of this line of code is replaced with native document.body.keydown (function () {
if (event.keyCode == "13") {//keyCode=13
document.getElementById("btnSumit").click();
}
});
Why did you alarm that document.body is not a function
the first error report is Uncaught TypeError: document.body.keydown is not a function
document.body .keydown is not a function, not document.body
secondly, keydown () is an instance method of a jQuery object, not a method on an dom instance. And to be honest, if the jQuery event binding specification is a little bit, it should be written as
.
$("body").on("keydown",function(e){
//
})
so
we should write
document.querySelector("body").addEventListener("keydown",function(e){
//
})
in order to have a chance to unbind later, it is best to give function a separate reference rather than an anonymous function
document.body.onkeydown = function(){
if (event.keyCode == "13") {//keyCode=13
document.getElementById('btnSumit').click();
}
}
Native js writing directly requires the on
prefix. onkeydown
, onclick
if it's addEventListener
, you don't need on
prefix
.
document.body.onkeydown = function(){
if (event.keyCode == "13") {//keyCode=13
document.getElementById('btnSumit').onclick();
}
}
------
document.body.addEventListener("keydown",function(){
if (event.keyCode == "13") {//keyCode=13
document.getElementById('btnSumit').onclick();
}
},false)