JavaScript 常用功能与 API 速查指南

作者:函数茶馆

时间:2026-10-26

浏览:463

本文系统梳理 JavaScript 开发中的核心常用功能。涵盖路径符号含义及根目录获取、URL 参数提取、Math 数学运算与随机数生成、数组合并与连接、正则表达式测试、表单事件处理(onSubmit)、Cookie 读写、定时器控制、窗口打开以及对象创建与销毁等关键知识点,提供完整代码示例与参数说明。

文章目录
1.路径符号的含义

路径符号含义与根目录获取

路径引用中,以斜杠开头的如 src="/js/jquery.js" 表示绝对路径,即从网站根目录开始定位资源。


其他形式如 "./ "、"../"、"jquery.js" 以及 "js/jquery.js" 等均为相对路径,其位置是相对于当前网页所在目录进行计算的。


2.获取网站的根目录

具体实现代码如下所示:

 
function GetRootPath() {
var strFullPath = window.document.location.href;
var strPath = window.document.location.pathname;
var pos = strFullPath.indexOf(strPath);
var prePath = strFullPath.substring(0, pos);
var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
return (prePath + postPath);
} 

3.获取url的参数

URL 参数提取方法

获取 URL 参数的具体代码如下:

 
//网站的 url如: http://www.A.COM?a=12
String.prototype.getQuery = function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = this.substr(this.indexOf("\?") + 1).match(reg);
if (r != null) return unescape(r[2]); return null;
} 
var strHref = window.location.href;
alert(strHref.getQuery("a"));

4. js中的函数

Math 数学对象与类型转换函数

4.1 Math.round 用于实现数值的四舍五入操作。


具体测试代码如下:

 
document.write(Math.round(0.60) + "<br />") 1
document.write(Math.round(0.50) + "<br />") 1
document.write(Math.round(0.49) + "<br />") 0
document.write(Math.round(-4.40) + "<br />") -4
document.write(Math.round(-4.60)) -5

4.2 Math.random() 方法用于返回一个介于 0 到 1 之间的随机浮点数。


示例代码如下:

 
document.write(Math.random())
document.write(Math.floor(Math.random()*11)) Math 对象的 floor() 方法和 random() 结合使用,可返回一个介于 0 和 10 之间的随机整数。

4.3 isNaN() 函数用于判断值是否是非数字,如果是非数字则返回 true,否则返回 false。


4.4 Number() 函数用于将对象的值转换为数字类型。


4.5 parseFloat() 和 parseInt() 函数在字符串第一个字符无法转换为数字时,会返回 NaN。


4.6 String() 函数用于将对象的值转换为字符串类型。


5.数组

数组操作与正则表达式

5.1 数组可以通过 concat 方法合并为一个新的数组,该操作不会改变原数组。


示例代码如下:

 
var arr = new Array(3)//定义数组
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
var arr1 = new Array(3)
arr1[0] = "James"
arr1[1] = "Adrew"
arr1[2] = "Martin"
var arr2=arr.concat(arr1))

5.2 数组可以通过 join 方法合并为字符串,默认使用逗号连接,也可以指定分隔符,例如 join(".")。


6. 正则表达式 最常用的是test(),找到是true,否则是false

正则表达式示例代码如下:

 
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));

7.事件

表单事件处理机制

7.1 onload 和 onUnload 事件分别在页面加载完成和卸载时触发调用。


7.2 onFocus、onBlur 和 onChange 事件通常相互配合,用于执行表单验证逻辑。


<input type="text" size="30" id="email" onchange="checkEmail()">


7.3 onSubmit 事件用于在提交表单之前验证所有的表单域。


具体应用示例如下:

 
/* 
下面是一个使用 onSubmit 事件的例子。当用户单击表单中的确认按钮时,checkForm() 函数就会被调用。假若域的值无效,此次提交就会被取消。checkForm() 函数的返回值是 true 或者 false。如果返回值为true,则提交表单,反之取消提交。 */
<form method="post" action="xxx.htm" onsubmit="return checkForm()">

8. cookie

Cookie 读写与定时器控制

8.1 Cookie 的创建方法如下:


具体实现代码如下:

 
function setCookie(c_name,value,expiredays)
{ 
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
} 

8.2 Cookie 的读取方法如下:


具体实现代码如下:

 
function getCookie(c_name)
{ 
if (document.cookie.length>0)
{ 
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{ 
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
} 
} 
return ""
} 

9. 计时

setTimeout() 用于启动计时器。


var t=setTimeout("javascript语句",毫秒) clearTimeout(t) //停止计时


10. 打开网站

窗口操作与对象创建销毁

10.1 使用 window.open() 在另一个窗口中打开网站。


具体实现代码如下:

 
function openW(v){
var str = 'width=200,height=200,left=200,top=200,status=no,scrollbars=no,'
str += 'menubar=no,toolbar=no,resizable=no,location=no'
window.open(v,'',str);
} 

10.2 在当前窗口中打开网站的方法。


window.location.href ='http://www.sohu.com' ;


11. 对象

11.1 对象的定义与销毁方式。


具体实现代码如下:

 
var oObject = new Object;
// do something with the object here
oObject = null;

11.2 通过构造函数定义类的方法。


具体实现代码如下:

 
function Cat(name,color){
this.name = name;
this.color = color;
this.type = "猫科动物";
this.eat = function(){alert("吃老鼠");};
  } 

11.3 利用 JSON 格式构造对象的方法。


具体实现代码如下:

 
var People = {
Create: function (name, age) {
this.name = name;
this.age = age;
}, 
SayHello: function () {
alert("Hello,My name is " + this.name + ".I am " + this.age);
} 
}; 

11.4 利用 prototype 属性构造对象的方法。


具体实现代码如下:

 
var Person = function (name, age) {
this.name = name;
this.age = age;
}; 
Person.prototype.Introduce = function () {
alert("My name is " + this.name + ".I'm " + this.age);
} 

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章