1.路径符号的含义
src="/UploadFiles/2021-04-02/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如: 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中的函数
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
8.1 创建
复制代码 代码如下:
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 读取
复制代码 代码如下:
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);
}
标签:
javascript,常用功能

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“javascript 常用功能总结”
暂无“javascript 常用功能总结”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。