首页 > 代码库 > Javascript第六天

Javascript第六天

//数组元素添加

//加到结尾push

var arr=[1,2,3,4,5,6]

arr.push(7,8,9);

document.write(arr+"<br />");

//加到开头unshift

arr.unshift(0);

document.write(arr+"<br />");

//加到指定位置splice 第一个数字表示位置,从0位开始;第二个数字0表示不替换掉该位置的值,原来的值自动向后移,1表示替换的个数。

arr.splice(1,0,"abc");

document.write(arr+"<br />");

//数组元素删除

var del=["abc",23,345,56,"bb"];

document.write(del+"<br />");

var last=del.pop();

document.write(last+"<br />");

document.write(del+"<br />");

var first=del.shift();

document.write(first+"<br />")

document.write(del+"<br />");

var arr=del.splice(0,2);

document.write(arr+"<br />");

document.write(del+"<br />");

 

 

//string对象(字符串)

//获取字符串长度

var str1="hello world",

str2="你好吗?";

document.write(str1.length+"&nbsp;"+str2.length+"<br />")

 

//提取字符串

//charAt() 返回指定位置字符串

var str = "HELLO WORLD",

n=str.charAt(2);

document.write(n+"<br />");

//substr() 传入起始位数,截取的长度

var p=str.substr(0,2);

document.write(p+"<br />");

//substring() 传入开始位置,结束位置小于...

var r=str.substring(0,3);

document.write(r+"<br />");

 

//查找替换字符串

//indexOf()和lastIndex()方法

var str="Hello world,welcome to the university."

document.write(str.indexOf("world")+"<br />");

document.write(str.lastIndexOf("c")+"<br />");

//replace()

var str="visit Microsoft!",

n=str.replace("Microsoft","W3CSchool");

document.write(n+"<br />");

//search()

var str="visit W3CSchool!",

n=str.search("W3CSchool");

document.write(n+"<br />");

 

//第一次出现o到最后一次出现o之间的字符串截取出来

var str="Hello world,welcome to the university.",

a=str.indexOf("o"),

b=str.lastIndexOf("o"),

d=str.substring(a+1,b);

document.write(d+"<br />");

 

//拼接字符

//concat

var str1="Hello ",

str2="world!",

n=str1.concat(str2);

document.write(n+"<br />");

//字符串拼接符

var str1=str1+str2;

document.write(str1+"<br />");

 

//其他

//转小写

var str="HEllo WoRlD!";

document.write(str.toLowerCase()+"<br />");

//转大写

var str="hellO worLd!";

document.write(str.toUpperCase()+"<br />");

 

 

 

var title="习大大主席出席二十国集团领导人杭州峰会系列活动纪实";

                 var a=title.substr(0,10);

                 document.write(a+”…”);

 

//确认对话框     confirm()

var result=confirm("你确认删除?");

                      console.log(result);

                 if (result==true) {

                      alert("成功删除");

                 } else{

                      alert("取消删除");

 

                 }

 

//设置循环定时器

          

                 //写法一

                 /*window.setInterval(function clock1 () {

                      console.log("I am boy")

                 },1000);*/

                 //清除定时器

                 var clock5=setInterval(function() {

                      console.log("I am boy");

                 },1000);

                 setTimeout(function(){

                      clearInterval(clock5);

                 },10000);

 

 

                 //写法二

                 /*setInterval(function clock2 () {

                     

                 },1000);

                 */

                 //写法三

                 /*function clock3 () {

                      console.log("I love u");

                 }

                 window.setInterval(clock3,1000);*/

                

           // 设置单次定时器

                 /*function clock4 () {

                      console.log("3秒大招");

                 }

                 window.setTimeout(clock4,3000);*/

 

//history对象

setTimeout(function  () {

                      history.back();

                 },3000);     

//location对象

setTimeout(function  () {

                      location.href="http://www.mamicode.com/demo1.html";

                 },3000)

 

//获取文档对象

var title=document.querySelector("#title");

                      //修改h1内容

                      title.innerHTML="我把h1标签内容和样式改了。";

                      //修改h1样式

                      title.style.color="red";

                      title.style.fontSize="60px";

                      title.style.textShadow="3px 3px rgba(21, 123, 114, .6)";

Javascript第六天