#Javascript# Javascript基本问题总结

在input框回车要触发的事宜和要领

//html
<input type="text" id="onkeyEvent"/>

//JS
document.getElementById('onkeyEvent').onkeypress = function () {
    if (event.keyCode === 13) {
        console.log('你点击了回车按钮!')
    }
};

HTML DOM confirm() 要领

confirm() 要领用于显现一个带有指定音讯和 OK 及作废按钮的对话框。

//语法
confirm(message)

假如用户点击肯定按钮,则 confirm() 返回 true。假如点击作废按钮,则 confirm() 返回 false。
在用户点击肯定按钮或作废按钮把对话框封闭之前,它将阻挠用户对浏览器的一切输入。在挪用 confirm() 时,将停息对 JavaScript 代码的实行,在用户作出相应之前,不会实行下一条语句。

//html
<div id="exit">退出</div>

//JS
document.getElementById('exit').onclick = function () {
    if (confirm('你肯定要退出么?')) {
       console.log('你点击了肯定按钮!');
    }
}

禁用浏览器的返回键

在经常使用浏览器中,都可以禁用了退却。然则IE8中不兼容。

//防备页面退却 不兼容IE8~9
(function(){
    if (window.history && window.history.pushState){
        history.pushState(null, null, document.URL);
        window.onpopstate = function (){
            history.pushState(null, null, document.URL);
        }
    }
})()

js猎取服务器地点

function getURL(){  
   var curWwwPath = window.document.location.href;  
   //猎取主机地点以后的目次,如: test/test/test.htm  
   var pathName = window.document.location.pathname;  
   var pos = curWwwPath.indexOf(pathName); //猎取主机地点,如: http://localhost:8080  
   var localhostPaht = curWwwPath.substring(0, pos); //猎取带"/"的项目名,如:/web
   var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);  
   var rootPath = localhostPaht + projectName;  
   return rootPath;  
     
}  
    原文作者:大煜儿
    原文地址: https://segmentfault.com/a/1190000018705177
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞