9-7
大小写转换
<script type="text/javascript"> var registor = "a123456", username = "A123456"; console.log( username.toLowerCase() ); </script>
定时器
setInterval(function() { console.log("I am setInterval"); }, 1000);
清除定时器
var clock = setInterval(function(){ console.log("I am clock"); }, 1000); setTimeout(function(){ clearInterval(clock); }, 10000)
截取字符串长度
<script type="text/javascript"> var title = "主席出席二十国集团领导人杭州峰会系列活动纪实"; document.write( title.substr(0, 10) + "..."); </script>
数组的增删改查
<script type="text/javascript"> var arr = ["orange", "mango", "banana", "sugar", "tea"]; console.log(arr); arr.splice(2, 0, "fish"); console.log(arr); arr.splice(5, 1); console.log(arr); </script>
字符串的替换
<script type="text/javascript"> var content = "abc"; console.log( content.replace("b", "q") ); </script>
打开一个新的页面
<script type="text/javascript"> window.open("http://www.baidu.com","_blank","width=300, height=200"); </script>
验证邮箱
<script type="text/javascript"> var email = "shann@wengdocomcn", atPos = email.indexOf("@"), dotPos = email.lastIndexOf("."); console.log(atPos, dotPos); if( atPos <= 0 || dotPos < atPos ){ alert("请输入正确的电子邮箱"); } </script>
弹出对话框
<script type="text/javascript"> var result = confirm("你确认删除?"); console.log(result); if( result ){ alert("正在删除中..."); }else{ alert("你取消了删除"); } </script>
拆分字符串
<script type="text/javascript"> var username = "a123456"; console.log(username.charAt(0)); for(var i = 0; i < username.length; i++) { document.write('<span class="font' + i + '">' + username.charAt(i) + "</span>"); } </script>
浏览记录后退
<script type="text/javascript"> setTimeout(function(){ history.back(); }, 3000); </script>
<h1 id="title">我是h1标题</h1> <script type="text/javascript"> // 1. 定义变量title // 2. 获取到页面内的#title的元素节点=>title // 3. 修改title的内容为“我已经被修改了。” // 4. 修改title的字体颜色为红色 // 1. 定义变量title // 2. 获取到页面内的#title的元素节点=>title var title = document.querySelector("#title"); // 3. 修改title的内容为“我已经被修改了。” title.innerHTML = "我已经被修改了。"; // 4. 修改title的字体颜色为红色 title.style.color = "red"; </script>