節點屬性
獲取節點屬性
getAttribute(屬性) 獲取屬性
通過這個方法,可以得到 某些元素的 某些屬性 。
alert(demo.getAttribute("id"));
alert(demo.getAttribute("class"));
alert(demo.getAttribute("title"));
彈出對話框: 彈出title里面的內容
設置節點屬性
setAttribute(“屬性”,”值”);
比如說,我們想要把 一個 類名 改為 demo
div.setAttribute(“class”,”demo”);
刪除某個屬性
removeAttribute(“屬性”);
demo.removeAttribute(“title”) ;
之后這個盒子就沒有title屬性 ,因為給刪掉了 。
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.mbox-one{
color: red;
}
</style>
<script type="text/javascript">
window.onload = function () {
var demo = document.getElementById("mbox");
console.dir(demo.getAttribute("id"));//mbox
console.dir(demo.getAttribute("class"));//box
console.dir(demo.getAttribute("title"));//mybox
demo.setAttribute("class","mbox-one");
demo.removeAttribute("title");
console.dir(demo.getAttribute("title"));//null
}
</script>
</head>
<body>
<div class="box" id="mbox" title="mybox">1235</div>
</body>
</html>
例:生成輪播圖的頁腳(張數)
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
body,ul,li{
margin: 0;
padding: 0;
}
.box{
width: 730px;
height: 454px;
margin: 100px auto;
position: relative;
}
ul,li{
list-style: none;
}
.box-img{
width: 730px;
height: 454px;
overflow: hidden;
}
.pager{
position: absolute;
bottom: 10px;
left: 50%;
margin-left: -75px;
}
.pager span{
display: inline-block;
width: 24px;
height: 24px;
text-align: center;
line-height: 24px;
background-color: #333;
border-radius: 12px;
color: #fff;
margin-left: 6px;
}
.pager span.current{
color: #fff;
background-color: #fff;
color: #333;
}
</style>
<script type="text/javascript">
window.onload = function () {
var box = document.getElementById("scroll");
var pagerBox = document.createElement("div");
pagerBox.className = "pager";
box.appendChild(pagerBox);
var ul = document.getElementsByTagName("ul")[0];
var lis = ul.children;
for(var i=0 ;i<lis.length ; i++){
var newSpan = document.createElement("span");
newSpan.innerHTML = i+1;
pagerBox.appendChild(newSpan);
}
var curSpan = pagerBox.children[1];//第二個被選中
// curSpan.className = "current";
curSpan.setAttribute("class","current");
}
</script>
</head>
<body>
<div class="box" id="scroll">
<div class="box-img">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
</body>
</html>
例:模擬發布微博
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
ul,li{
margin: 0;
padding: 0;
}
a{
text-decoration: none;
}
.box{
width: 550px;
margin: 0 auto;
border: 1px solid #333;
}
textarea{
width: 350px;
height: 120px;
resize: none;
margin-top: 6px;
}
li{
list-style: none;
border-bottom: 1px dotted #999;
margin: 5px 25px 5px 62px;
}
li a{
float: right;
}
</style>
<script type="text/javascript">
window.onload = function () {
var box = document.getElementById("box");
var btn = box.getElementsByTagName("button")[0];
var txt = box.getElementsByTagName("textarea")[0];
var ul = document.createElement("ul");
box.appendChild(ul);
btn.onclick = function () {
if(txt.value == ""){
alert("輸入不能為空");
}else{
//創建一個li
var newLi = document.createElement("li");
newLi.innerHTML = txt.value+"<a href='javascript:;'>刪除</a>";
//清空輸入框
txt.value = "";
var lis = ul.children;
if(lis.length == 0){//第一個直接插入
ul.appendChild(newLi);
}else{//以后每個都要插在第一個前面
ul.insertBefore(newLi,lis[0]);
}
//刪除
var as = ul.getElementsByTagName("a");
for(var i=0;i<as.length ;i++){
as[i].onclick = function () {
var delLi = this.parentNode;
ul.removeChild(delLi);
}
}
}
}
}
</script>
</head>
<body>
<div class="box" id="box">
微博發布 <textarea></textarea> <button>發布</button>
</div>
</body>
</html>
JS內置對象
|對象名稱|對象說明|
|::|::|
|Arguments|函數參數集合|
|Array|數組對象|
|Boolean|布爾對象|
|Date|日期時間|
|Error|異常對象|
|Function|函數構造器|
|Math|數學對象|
|Number|數值對象|
|Object|基礎對象|
|RegExp|正則表達式對象|
|String|字符串對象|
|Event|事件對象|
日期函數 ( Date() )
這個函數 (對象) 可以設置我們本地 日期。 年月日 時分秒
聲明日期
var date = new Date();
創造聲明一個新的日期函數 賦值給了 date
var arr = new Array();
使用函數
|方法| 描述|
|::|::|
|Date() |返回當日的日期和時間。
|getDate() |從 Date 對象返回一個月中的某一天 (1 ~ 31)。|
|getDay() |從 Date 對象返回一周中的某一天 (0 ~ 6)。|
|getMonth()| 從 Date 對象返回月份 (0 ~ 11)。|
|getFullYear()| 從 Date 對象以四位數字返回年份。|
|getYear() |請使用 getFullYear() 方法代替。|
|getHours() |返回 Date 對象的小時 (0 ~ 23)。|
|getMinutes() |返回 Date 對象的分鐘 (0 ~ 59)。|
|getSeconds() |返回 Date 對象的秒數 (0 ~ 59)。|
|getMilliseconds()| 返回 Date 對象的毫秒(0 ~ 999)。|
|getTime() |返回 1970 年 1 月 1 日至今的毫秒數。|
|getTimezoneOffset()| 返回本地時間與格林威治標準時間 (GMT) 的分鐘差。|
|getUTCDate() |根據世界時從 Date 對象返回月中的一天 (1 ~ 31)。|
|getUTCDay() |根據世界時從 Date 對象返回周中的一天 (0 ~ 6)。|
|getUTCMonth() |根據世界時從 Date 對象返回月份 (0 ~ 11)。|
|getUTCFullYear() |根據世界時從 Date 對象返回四位數的年份。|
|getUTCHours() |根據世界時返回 Date 對象的小時 (0 ~ 23)。|
|getUTCMinutes() |根據世界時返回 Date 對象的分鐘 (0 ~ 59)。|
|getUTCSeconds() |根據世界時返回 Date 對象的秒鐘 (0 ~ 59)。|
|getUTCMilliseconds() |根據世界時返回 Date 對象的毫秒(0 ~ 999)。|
|parse() |返回1970年1月1日午夜到指定日期(字符串)的毫秒數。|
|setDate() |設置 Date 對象中月的某一天 (1 ~ 31)。|
|setMonth() |設置 Date 對象中月份 (0 ~ 11)。|
|setFullYear() |設置 Date 對象中的年份(四位數字)。|
|setYear() | 請使用 setFullYear() 方法代替。|
|setHours() |設置 Date 對象中的小時 (0 ~ 23)。|
|setMinutes() |設置 Date 對象中的分鐘 (0 ~ 59)。|
|setSeconds() |設置 Date 對象中的秒鐘 (0 ~ 59)。|
|setMilliseconds()| 設置 Date 對象中的毫秒 (0 ~ 999)。|
|setTime() |以毫秒設置 Date 對象。|
|setUTCDate() |根據世界時設置 Date 對象中月份的一天 (1 ~ 31)。|
|setUTCMonth() |根據世界時設置 Date 對象中的月份 (0 ~ 11)。|
|setUTCFullYear() |根據世界時設置 Date 對象中的年份(四位數字)。|
|setUTCHours() |根據世界時設置 Date 對象中的小時 (0 ~ 23)。|
|setUTCMinutes() |根據世界時設置 Date 對象中的分鐘 (0 ~ 59)。|
|setUTCSeconds() |根據世界時設置 Date 對象中的秒鐘 (0 ~ 59)。|
|setUTCMilliseconds()| 根據世界時設置 Date 對象中的毫秒 (0 ~ 999)。|
|toSource() |返回該對象的源代碼。|
|toString() |把 Date 對象轉換為字符串。|
|toTimeString()| 把 Date 對象的時間部分轉換為字符串。|
|toDateString() |把 Date 對象的日期部分轉換為字符串。|
|toGMTString()| 請使用 toUTCString() 方法代替。|
|toUTCString() |根據世界時,把 Date 對象轉換為字符串。|
|toLocaleString() |根據本地時間格式,把 Date 對象轉換為字符串。|
|toLocaleTimeString()| 根據本地時間格式,把 Date 對象的時間部分轉換為字符串。|
|toLocaleDateString()| 根據本地時間格式,把 Date 對象的日期部分轉換為字符串。|
|UTC() |根據世界時返回 1970 年 1 月 1 日 到指定日期的毫秒數。|
|valueOf()| 返回 Date 對象的原始值。|
例:日歷實現
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.box{
width: 200px;
height: 200px;
margin: 0 auto;
border: 1px solid #ccc;
background-color: green;
}
.box span{
display: block;
width: 100px;
height: 100px;
background: yellowgreen;
text-align: center;
line-height: 100px;
margin: 10px auto;
font-size: 50px;
}
.box p{
text-align: center;
color: #000;
}
</style>
<script type="text/javascript">
window.onload = function () {
var box = document.getElementById("date");
var childs = box.children;
//日期函數
var date = new Date();
childs[1].innerHTML = date.getDate();//一個月中的日期
var dateStr = ["日","一","二","三","四","五","六"];
var str = date.getFullYear()+"年"+date.getMonth()+"月"+date.getDate()+"日 星期"+dateStr[date.getDay()];
childs[0].innerHTML = str;
}
</script>
</head>
<body>
<div class="box" id="date">
<p>2015-11-24 星期五</p>
<span>13</span>
</div>
</body>
</html>
定時器 setInterval
很多情況下,一些操作不需要人工干預, 代碼自己會不斷的去執行 。而且 會有 時間的綁定 。 比如每隔 5秒鐘就去執行一次事件。
我們可以設定時間 讓某個動作不斷的去執行 。 這個我們再js 里面用定時器來表示。
window.setInterval(“執行的函數”,間隔時間)
正確的寫法:setInterval(fun, 1000);
每隔1秒鐘(1000ms),就去執行一次 fun 這個函數.
setInterval(“fun()”,1000);
可以這樣寫
setInterval( function(){} , 1000 ) ;
錯誤的寫法setInterval(fun(),1000);
例:定時器
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
window.onload = function () {
// var doubleDate = new Date("2107/11/11 00:00:00");
var demo = document.getElementById("demo");
setInterval(func , 1000);
var num = 0;
function func(){
num++;
demo.innerHTML = num;
}
}
</script>
</head>
<body>
<div id="demo">
0
</div>
</body>
</html>
定義自己的日期時間
var endTime = new Date(“2015/12/12”);`
- 如果date 括號里面寫日期時間 就是自己定義的日期時間
new Date(“2015/12/12 17:30:00”);
自定義日期時間 - 如果 date括號里面不寫日期時間 , 就是當前日期時間 。
new Date()
當前日期時間
日期和時分秒中間 有空格隔開
例:倒計時
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.demo{
/*display: inline-block;*/
/*width: 500px;*/
margin: 100px auto;
font-size: 36px;
color: red;
font-family: "simsun";
text-align: center;
}
</style>
<script type="text/javascript">
window.onload = function () {
var demo = document.getElementById("demo").getElementsByTagName("span")[0];
var endTime = new Date("2017/6/9 17:30:00");
setInterval(clock,1000);
function clock(){
var now = new Date();
var second = parseInt((endTime.getTime()-now.getTime())/1000);
//天數
var day = parseInt(second/(60*60*24));
day = day>9?day:("0"+day);
//小時數
var hour = parseInt((second/(60*60))%24);
hour = hour>9?hour:("0"+hour);
//分鐘
var min = parseInt(second/60%60);
min = min>9?min:("0"+min);
//秒
var sec = parseInt(second%60);
sec = sec>9?sec:"0"+sec;
demo.innerHTML = day+"天"+hour+"時"+min+"分"+sec+"秒";
}
}
</script>
</head>
<body>
<div id="demo" class="demo">
距離下一次放假還有:<span>00天00時00分00秒</span>
</div>
</body>
</html>
例:時鐘
- 要得到現在的 時、分、秒
這里面有一個小玄機,比如現在是 9點整時針指向9是沒錯的 ,但是如果現在是 9點半 時針應該指向的是 9到10 之間才對。所以,我們不但要得到現在的小時,還要得到已經過去了多少分。
ms = date.getMilliseconds(); // 現在的毫秒數
s = date.getSeconds() + ms / 1000; // 得到秒 1.3 s
m = date.getMinutes() + s / 60; // 得到的是分數 45.6分鐘
h = date.getHours() % 12 + m / 60 ;
秒針在一格一格的跳,完整代碼:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.clock{
width: 600px;
height: 600px;
background: url("images/clock.jpg") no-repeat center;
margin: 0 auto;
position: relative;
}
.clock div{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
.clock .h{
background: url("images/hour.png") no-repeat center;
}
.clock .m{
background: url("images/minute.png") no-repeat center;
}
.clock .s{
background: url("images/second.png") no-repeat center;
}
</style>
<script type="text/javascript">
window.onload = function () {
var hour = document.getElementById("hour");
var minute = document.getElementById("minute");
var second = document.getElementById("second");
var h=0, m = 0,s = 0; ms = 0;
//一開始刷新一次,因為定時器要一秒之后再執行
refreshClock();
//定時器
setInterval(function () {
refreshClock();
} , 1000);
function refreshClock(){
//得到當前時間
var now = new Date();
s = now.getSeconds();
//分針的旋轉角度和分鐘、秒鐘數有關
m = now.getMinutes() + s/60;
//時針的旋轉角度和小時、分鐘數有關
h = now.getHours() + m/60;
second.style.webkitTransform = "rotate("+s*6+"deg)";
minute.style.webkitTransform = "rotate("+m*6+"deg)";
hour.style.webkitTransform = "rotate("+h*60+"deg)";
}
}
</script>
</head>
<body>
<div class="clock">
<div class="h" id="hour"></div>
<div class="m" id="minute"></div>
<div class="s" id="second"></div>
</div>
</body>
</html>
秒針平滑的過渡,完整代碼:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.clock{
width: 600px;
height: 600px;
background: url("images/clock.jpg") no-repeat center;
margin: 0 auto;
position: relative;
}
.clock div{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
.clock .h{
background: url("images/hour.png") no-repeat center;
}
.clock .m{
background: url("images/minute.png") no-repeat center;
}
.clock .s{
background: url("images/second.png") no-repeat center;
}
</style>
<script type="text/javascript">
window.onload = function () {
var hour = document.getElementById("hour");
var minute = document.getElementById("minute");
var second = document.getElementById("second");
var h=0, m = 0,s = 0; ms = 0;
//一開始刷新一次,因為定時器要一秒之后再執行
refreshClock();
//定時器
setInterval(function () {
refreshClock();
} , 10);
function refreshClock(){
//得到當前時間
var now = new Date();
ms = now.getMilliseconds();
s = now.getSeconds()+ms/1000;
//分針的旋轉角度和分鐘、秒鐘數有關
m = now.getMinutes() + s/60;
//時針的旋轉角度和小時、分鐘數有關
h = now.getHours() + m/60;
second.style.webkitTransform = "rotate("+s*6+"deg)";
minute.style.webkitTransform = "rotate("+m*6+"deg)";
hour.style.webkitTransform = "rotate("+h*60+"deg)";
}
}
</script>
</head>
<body>
<div class="clock">
<div class="h" id="hour"></div>
<div class="m" id="minute"></div>
<div class="s" id="second"></div>
</div>
</body>
</html>
this
-
在事件注冊的函數中
this 指向的是事件的調用者
-
在普通函數中
this指的是函數的使用者。
一般情況下,我們喜歡 var that = this;
var that = this; // 把 btn 對象 給 that ,或者var _this = this;
關閉定時器
clearInterval(定時器名字);
例:發送短信的倒計時
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
window.onload = function () {
var btn = document.getElementsByTagName("button")[0];
var count;
var that;
var timer = null;
btn.onclick = function () {
//先清除原來的定時器
if(timer) clearInterval(timer);
count = 10;
// btn.disabled = true;
btn.setAttribute("disabled","true");
timer = setInterval(sendTxtMsg,1000);
that = this;
}
function sendTxtMsg(){
// console.log(this);
// console.log(that);
if(count<=0){
clearInterval(timer);
that.innerHTML = "重新發送短信";
that.disabled = false;
}else{
that.innerHTML = "還剩"+count+"s發送";
count--;
}
}
}
</script>
</head>
<body>
<input type="text"/><button>發送短信</button>
</body>
</html>
定時器setTimeout
多少時間后執行,之執行一次
setTimeout(“函數”, 時間 ) ;
setInterval(fn,5000);
每隔 5秒鐘,就去執行函數fn一次
setTimeout(fn,5000);
5秒鐘之后,去執行 fn 函數, 只執行一次
例:5秒之后關閉廣告
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#left{
position: fixed;
top: 240px;
left: 0;
}
#right{
position: fixed;
top: 240px;
right: 0;
}
</style>
<script type="text/javascript">
window.onload = function () {
function $(id) {return document.getElementById(id);}
function hide(id){
$(id).style.display = "none";
}
setTimeout(closeAD,5000);
function closeAD(){
hide("left");
hide("right");
}
}
</script>
</head>
<body>


</body>
</html>
深層次的看待定時器區別
- setInterval是排隊執行的
假如 間隔時間是1秒, 而執行的程序的時間是2秒 上次還沒執行完的代碼會排隊, 上一次執行完下一次的就立即執行, 這樣實際執行的間隔時間為2秒 - setTimeout
setTimeout延遲時間為1秒執行, 要執行的代碼需要2秒來執行,那這段代碼上一次與下一次的執行時間為3秒.
JS頁面跳轉
JS 頁面跳轉: window.location.href = ”http://www.itcast.cn” ;
屬于BOM對象的方法
例:5秒后跳轉到百度首頁
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
window.onload = function () {
var demo = document.getElementById("demo");
var count = 5;
var speed = 1000;
setTimeout(getIndexPager , speed);//1秒之后執行getIndexPager函數
function getIndexPager(){
count--;
if(count<0){
window.location.
}else{
setTimeout(getIndexPager,speed);
demo.innerHTML = (count+1)+"秒之后跳轉到百度";
}
}
}
</script>
</head>
<body>
<span id="demo">5秒之后跳轉到百度</span>
<a >首頁</a>
</body>
</html>
如上所示,函數自己調用自己的過程 我們稱之為 : 遞歸調用,遞歸調用必須有退出條件
arguments對象
function fn(a,b,c) { console.log(a+b+c); alert(arguments.length;)}
fn(1,3,4,6);
arguments只在正在使用的函數內使用。
返回正在執行的函數:
arguments.callee;
返回的是正在執行的函數。 也是在函數體內使用。 在使用函數遞歸調用時推薦使用arguments.callee代替函數名本身。
function fn() { console.log(arguments.callee); }
這個callee 就是 : function fn() { console.log(arguments.callee); }
所以上面的遞歸調用:setTimeout(getIndexPager,speed);
可以改為:setTimeout(arguments.callee,speed);
例:圖片滾動展示動畫
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.box{
width: 512px;
height: 400px;
overflow: hidden;
margin: 100px auto;
border: 2px solid #999;
position: relative;
}
.box img{
position: absolute;
left: 0;
top: 0;
}
.box .up{
position: absolute;
width: 100%;
height: 200px;
top: 0;
left: 0;
}
.box .down{
position: absolute;
width: 100%;
height: 200px;
left: 0;
bottom: 0;
}
</style>
<script type="text/javascript">
window.onload = function () {
function $(id){return document.getElementById(id);}
var spans = document.getElementsByTagName("span");
var pic = $("pic");
var num = 0;
var upTimer = null;
var downTimer = null;
spans[0].onmouseover = function () {
if(upTimer) clearInterval(upTimer);
upTimer = setInterval(function(){
if(num > -(pic.height-400) ){
num-=3;
pic.style.top = num+"px";
}else{
//清除定時器
clearInterval(upTimer);
}
},10);
}
spans[0].onmouseout = function () {
if(upTimer) clearInterval(upTimer);//清除向上滾的定時器
}
spans[1].onmouseover = function () {
if(downTimer) clearInterval(downTimer);
downTimer = setInterval(function () {
if(num<0){
num++;
pic.style.top = num+"px";
}else{
clearInterval(downTimer);
}
},10);
}
spans[1].onmouseout = function () {
if(downTimer) clearInterval(downTimer);//清除向下滾的定時器
}
}
</script>
</head>
<body>
<div class="box">

<span class="up"></span>
<span class="down"></span>
</div>
</body>
</html>
Javascript單線程
Javascript是單線程執行的,也就是無法同時執行多段代碼,當某一段代碼正在執行的時候,所有后續的任務都必須等待,形成一個隊列,一旦當前任務執行完畢,再從隊列中取出下一個任務,這也常被稱為阻塞式執行。
如果代碼中設定了一個setTimeout
,那么瀏覽器便會在合適時間將代碼插入任務隊列,如果這個時間為0 ,便會立即插入任務隊列,但不是立即執行,仍然要等待前面的代碼執行完畢,所以setTimeout并不能保證執行的時間,是否及時執行取決于JavaScript的線程是擁擠還是空閑。
字符串對象
我們工作中經常進行字符串操作。
轉換為字符串
- 字符串加法:
2+ “” = “2” 2+”ab” = “2ab”
- String() 轉換為字符串
toString(基數);
基數就是進制
var txt = 10;
txt.toString(2) 二進制 1010
獲取字符位置方法
-
charAt
,獲取相應位置字符(參數: 字符位置) -
charCodeAt
獲取相應位置字符unicode編碼(參數: 字符位置) -
var txt = “abcedf”;
比如, txt.charAt(4); 索引號一定是從0開始 返回的結果是 d
我們根據我們輸入的 位數 返回相應的 字符 。
unicode編碼 是我們字符的字符的唯一表示 。
<script type="text/javascript">
var txt1 = "haha";
var txt2 = "你好";
alert(txt1.charAt(1));//a
alert(txt2.charAt(1));//好
alert(txt1.charCodeAt(1));//97
alert(txt2.charCodeAt(1));//22907
</script>
例:檢測字符串的長度,中文占兩個字符
<script type="text/javascript">
var txt = "what are you 弄啥咧!";
function getStringLen(str){
var len = 0;//存儲字符串的總長度
var c =0;//存儲每個字符的編碼
for(var i=0;i<str.length;i++){
c = str.charCodeAt(i);
if(c>=0 && c<=127){
len++;
}else{
len+=2;
console.log(c);
}
}
return len;
}
alert(getStringLen(txt));
</script>