javascript 常用功能总结
发布时间:2023-07-02 03:50:17
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如: 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);
}
猜你喜欢
- 感觉这个东西很常用,很多其它js框架里有,但是太大了,用不了太多,自己写了个。需要的朋友可以参考下。下面是html测试代码:<!DOC
- SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它
- 直接使用word文档已经难不倒大家了,有没有想过用python构建一个word文档写点文章呢?当然这个文章的框架需要我们用代码一点点的建立,
- requests模块是一个用于访问网络的模块,其实类似的模块还有很多,不在一一在这里解释。这么多的相似的模块为什么都说只有这个好用呢,因为他
- 介绍我们一起来做个示例,在.NET中新建一个类,并在这个类里新建一个方法,然后在SQL Server中调用这个方法。按照微软所述,通过宿主
- #/usr/bin/env/python#coding=utf-8import sys,re,time,osmaxdata = 50000
- 下面是调用方式:Example script - pymssql module (DB API 2.0) Example script -
- 一. valid卷积的梯度我们分两种不同的情况讨论valid卷积的梯度:第一种情况,在已知卷积核的情况下,对未知张量求导(即对张量中每一个变
- CentOS 7上PHP默认是以apache或者nobody的身份运行的,这种方式下由于PHP运行需要的权限比较大,会有安全隐患,还可能会受
- pytest介绍pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:1、简单灵活,容易上手,文档丰富;2、支持参数
- 创建:list = [5,7,9]取值和改值:list[1] = list[1] * 5列表尾插入:list.append(4)去掉第0个值
- 一、爬取豆瓣热评该程序进行爬取豆瓣热评,将爬取的评论(json文件)保存到与该python文件同一级目录 * 意需要下载这几个库:reques
- 一、基本概念(查询语句)①基本语句1、“select * from 表名;”,—
- 分享几个字体设计:1.巧克力2.绿野邂逅是人间四月天3.去年花开的时候4.仲夏梦之夜5.芊芊茉莉香6.玫瑰人生7.真情告白8.遮掩锋芒
- Python 内置的 zipfile 模块可以对文件(夹)进行ZIP格式的压缩和读取操作。要进行相关操作,首先需要实例化一个 ZipFile
- Data URIData URI是由RFC 2397定义的一种把小文件直接嵌入文档的方案。通过如下语法就可以把小文件变成指定编码直接嵌入到页
- 中间件介绍中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与
- 获取操作系统的当前运行状态和负载情况,是一个系统管理员的基本技能,因为这对我们日常排查故障,定位问题有着非常紧密的联系,比如查看当前系统的基
- 安装好jupyter notebook后,在pycharm中无论运行什么样的python脚本,都会默认使用python的console运行,
- scratch-blocks编译的时候会出现的问题:scratch-gui依赖的scratch-blocks模块在安装的时候编译会报错。原因