本文为我学习js的笔记,刚刚找到的学习资料,让我眼前一亮,在此处记载一些我比较感兴趣的资料,更多资料参考转载地址:http://www.shouce.ren/api/view/a/12180
1.创建对象,以前我还真不知道,才学会,分享一下。
function person(name,age){
this.name = name;
this.age = age;
}
Jim = new person("Jim",20);
document.write("Jim 的姓名是:",Jim.name,"
");
document.write("Jim 的年龄是:",Jim.age,"
");
Tom = new person();
Tom.name = "Tom";
Tom.age = 22;
document.write("Tom 的姓名是:",Tom.name,"
");
document.write("Tom 的年龄是:",Tom.age,"
");
2.创建随机数。
for(i=1;i<=5;i++){
num = Math.floor(Math.random() * 10);
document.write(num," ");
}
3.规定时间后执行一次。
function showtxt(){
a = txt.value;
txt.value = a + "*";
}
setTimeout("showtxt()",2000);
4.规定时间后反复执行。
function showtxt(){
a = txt.value;
txt.value = a + "*";
}
setInterval("showtxt()",1000);
5.捕获异常。
try{
a[1] = 10;
}catch(e){
alert("错误:"+e);
}
6.获得变量数据类型
name = "刘洪辉";
age = 22;
document.write('name变量的类型是:',typeof(name),'
');
document.write('age变量的类型是:',typeof(age),'
');
document.write('document的类型是:',typeof(document));
7.检查变量是否存在
a = "String";
b = 10;
str = typeof(a) == 'undefined' ? '不存在':'存在'
document.write('变量a:',str,'
');
str = typeof(b) == 'undefined' ? '不存在':'存在'
document.write('变量b:',str,'
');
str = typeof(c) == 'undefined' ? '不存在':'存在'
document.write('变量c:',str,'
');
8.将字符串转化成数字
str = "3.14";
num = parseFloat(str);
document.write(0 + num);
9.使用window来判断对象是否存在
function person(){}
Jim = new person();
if(window.Jim){
document.write("Jim对象存在");
}else{
document.write("Jim对象不存在");
}
10.获取当前网页的url地址
url = window.location.href;
document.write(url);
var password
password = prompt("请输入你的密码:","");
if (password != "123456"){
alert("输入的密码不正确,你不能看本页的内容");
window.location.href = "http://www.baidu.com";
}else{
alert("验证通过!");
}
12.给页面设置快捷键
function getkey(){
url = "http://www.yahoo.com.cn";
asc = event.keyCode;
key = String.fromCharCode(asc);
if(key == "G"){
ret = confirm("您要前往" + url + "页面吗?");
if(ret){
window.location = url;
}
}
}
document.onkeydown=getkey;
13.禁止页面鼠标右键
绝对禁止用鼠标右键
14.鼠标可以拖动文字
可以用鼠标拖动的文字