1、DOM(document object model文件对象模型)加载完毕后执行:
$(function() {
//具体操作
});
案例(监听鼠标点击):
$(function () {
document.onkeydown=keyDownSearch;
function keyDownSearch(e) {
let theEvent = e || window.event;
let code = theEvent.keyCode || theEvent.which || theEvent.charCode;
if(code==13){
$('#login').click();
/*根据id选择器监听点击事件,触发选择的组件执行onclick*/
$('#register').click();
$('#storePassword').click();
$('#autoLogins').click();
return false;
}
return true;
}
})
2、页面加载时监听函数:
window.onload=function (){
//具体操作
}
案例(记住用户名和密码功能,这里只有加载时监听的部分代码,想完成整个监听要修改补充):
window.onload=function (){
let oName = window.localStorage.getItem("LUsername");
//利用本地存储根据键值对拿值
let oPass = window.localStorage.getItem("LPassword");
let oStored = window.localStorage.getItem("stored");
if(oPass==null||oName==null||oStored==null){
}else{
let name = document.getElementById('LUsername');
//根据id拿到输入框对象
let pass = document.getElementById('LPassword');
let storedPassword = document.getElementById('storePassword');
pass.value =oPass;
//将输入框对象设置为从本地存储中拿到的值
name.value = oName;
if(oStored){
storedPassword.checked="checked";
stored=true;
}else {
}
}
}