js对象之String对象
创建string对象
var str = "123123";
stirng的属性
length:字符串长度
与html相关的对象
var str = "123";
document.write(str.length);
document.write("<hr>");
/字体加粗
document.write(str.bold());
document.write("<hr>")
/字体颜色
document.write(str.fontcolor("red"));
document.write("<hr>");
/字体大小(1-7有效值,大于7为7,小于1为1,除了-1很奇怪比1还大一点)
document.write(str.fontsize(7));
document.write(str.fontsize(7));
document.write("<hr>");
/设为超链接,还可以链接到别的路由
document.write(str.link("url"));
/文字下标
document.write(str.sub());
document.write(str);
/文字下标
document.write(str.sup());
结果:
/字符串连接
document.write(str1.concat(str2));
document.write("<hr>");
/获得字符
document.writeln(str1.charAt(0));
document.writeln(str2.charAt(1));
document.write("<hr>");
/获得字符串索引
document.writeln(str1.indexOf("3"));
document.writeln(str2.indexOf("a"));
document.write("<hr>");
var str3 ="a-b-c-d";
var arr = str3.split("-");
/切割字符串为字符数组
document.write(new Array(new String("a-b-c-d").split("-")).toString());
arr.forEach(a=>{
document.write(a);
})
document.write("<hr>");
/替换字符
document.write(str1.replace("123","a"));
document.write("<hr>");
/截取从 索引0开始的后3个字符
document.write(str1.substr(0,3));
document.write("<hr>");
/截取从 索引1开始到索引2的字符 不包括索引2字符 ,[1,2)
document.write(str2.substring(1,2));
结果:
js对象之Array对象
var arr1 =["简自豪","严君泽","刘世宇"];
var arr2 =["RNG","EDG","IG"];
//改变的是两个数组的副本,数组本身没有变化
document.write(arr1.concat(arr2));
document.write("<br>");
document.write(arr1);
document.write("<br>");
//使得数组分隔符由,变为+
document.write(arr1.join("+"));
document.write("<br>");
//添加一个或多个元素进数组,返回当前数组长度
document.write(arr1.push("史森明")+" ");
document.write(arr1);
//注意:如果添加的是一个数组那么会把这个数组的所有元素当做是一个字符串添加到数组中,也就是数组元素增加一个
// 长度只会加1
document.write("<br>");
document.write(arr1.push(arr2)+" ");
document.write(arr1);
document.write("<br>");
//删除并返回数组的最后一个元素
document.write("old :"+arr1);
document.write("<br>");
document.write(arr1.pop());
document.write("<br>");
document.write("new :"+arr1);
//颠倒数组中元素的顺序
document.write("<br>");
document.write(arr1.reverse());
结果:
Math对象
var num = 10.7;
//向上取整
document.write(Math.ceil(10.7)+"<br>");
//向下取整
document.write(Math.floor(10.7)+"<br>");
//四舍五入
document.write(Math.round(10.7)+"<br>");
//返回 0 ~ 1 之间的随机数。
document.write(Math.random()+"<br>");
//返回 0 ~ 9的随机数
document.write(Math.floor(Math.random()*10)+"<br>");
//返回 2 的 5 次幂。
document.write(Math.pow(2,5)+"<br>");
结果:
Date对象
var date = new Date();
//根据本地时间格式,把 Date 对象的日期部分转换为字符串。
document.write(date.toLocaleDateString()+"<br>");
//从 Date 对象以四位数字返回年份。
document.write(date.getFullYear()+"<br>");
//一年的第几个月(0~11)
document.write(date.getMonth()+"<br>");
//一周的第几天(外国人喜欢把星期天当做一周的第一天,0~6,1代表星期一)
document.write(date.getDay()+"<br>");
//一个月的第几天(1~31)
document.write(date.getDate()+"<br>");
//时
document.write(date.getHours()+"<br>");
//分
document.write(date.getMinutes()+"<br>");
//秒
document.write(date.getSeconds()+"<br>");
结果: