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);
}
猜你喜欢
- 本文实例为大家分享了Django实现文件上传下载的具体代码,供大家参考,具体内容如下一、django实现文件下载(1)、后台接口如果从服务器
- 如下所示:'''Created on 2018-4-20例子:每天凌晨3点执行func方法''
- JS中作用域的概念: 表示变量或函数起作用的区域,指代了它们在什么样的上下文中执行,亦即上下文执行环境。Javascript的作用域只有两种
- 主备同步,也叫主从复制,是MySQL提供的一种高可用的解决方案,保证主备数据一致性的解决方案。在生产环境中,会有很多不可控因素,例如数据库服
- 事件是什么 ?JavaScript与HTML交互是通过在用户或浏览器操纵页面上发生的事件进行处理。当页面加载,这是一个事件。当用
- 由于工作需要本文主结合了excel表格,对表格中的ssh密码进行批量修改以下是详细代码(python3):'''遇到
- 本文实例讲述了Thinkphp5框架实现获取数据库数据到视图的方法。分享给大家供大家参考,具体如下:这是学习thinkhp5的基础篇笔记。这
- 本文介绍了Vue2 SSR 缓存 Api 数据,分享给大家,具体如下:1. 安装缓存依赖: lru-cachenpm install lru
- 我们有时请求服务器时,无论get或post请求,会出现403错误,这是因为服务器拒绝了你的访问,这时我们可以通过模拟浏览器的头部信息进行访问
- 1、字符串前加 u例:u"我是含有中文字符组成的字符串。"作用:后面字符串以 Unicode 格式 进行编码,一般用在中
- 我们常常会用到PHP过滤一些标签的功能,比如过滤链接标签、过滤script标签等等,下面就介绍一下PHP过滤常用标签的正则表达式代码:$st
- 这篇文章主要介绍了python调用摄像头的示例代码,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下一、打开摄像头import
- declare @PageNumber int declare @PageSize int set @PageNumber=2 set @P
- 根据官网的文档,要在一个html文件下使用layui里面的组件库其实很简单,但是在vue项目中使用该ui库却存在着很多坑,下面我们就详细讲解
- 前言当我们运行测试函数时,我们希望确保测试函数在运行结束后,可以自己清理掉对环境的影响。这样的话,它们就不会干扰任何其他的测试函数,更不会日
- 概述虽然Python的强项在人工智能,数据处理方面,但是对于日常简单的应用,Python也提供了非常友好的支持(如:Tkinter),本文主
- 在进行PHP编程时,需要对服务器某个目录下面的文件进行浏览,通常成为遍历目录。取得一个目录下的文件和子目录,就需要用到opendir()函数
- 前言项目开发中,产品经理提了这样一个需求:将系统中的附件实现批量打包下载功能。本来系统中是有单个下载及批量下载功能,现在应业务方的需求,需要
- 我所希望的iframe功能是不要去控制父页面,如果他想要控制,哪从父页面里面加入什么代码能让他无法控制呢?假如iframe里面有这样的语句:
- onmouseout 发现它的触发太敏感,当经过层内文字链时,即触发onmousetout事件,功能不能正常显示,经过一番搜索,整理出来,供