js判断页面某个方法是否存在

以按下回车键为触发条件:

/*绑定键盘回车事件*/
$("body").keydown(function () {
    //谷歌能识别event,火狐识别不了,所以增加了这一句,chrome浏览器可以直接支持event.keyCode
    var theEvent = window.event || arguments.callee.caller.arguments[0];
    if (theEvent.keyCode == "13") {//keyCode=13是回车键
        try{
            if(query && typeof(query) == "function"){
                query();
                // console.log("存在该方法")
            }
        }catch(e){
            console.log("方法不存在");
        }
    }
});

function query() {
  console.log("刷新页面");
}

  

原文地址:https://www.cnblogs.com/sherryweb/p/14845433.html