toString() 用法:<數值變量>.toString();返回:字符串形式的數值。如:若
a == 123;則 a.toString() == '123'。
String 字符串對象。聲明一個字符串對象最簡單、快捷、有效、常用的方法就
是直接賦值。
length 用法:<字符串對象>.length;返回該字符串的長度。
charAt() 用法:<字符串對象>.charAt(<位置>);返回該字符串位于第<位置>
位的單個字符。注 意:字 符串中的一個字符是第 0 位的,第 二個才是第 1 位的,
最后一個字符是第 length - 1 位的。
charCodeAt() 用法:<字符串對象>.charCodeAt(<位置>);返回該字符串位于第
<位置>位的單個字符的 ASCII 碼。
fromCharCode() 用法:String.fromCharCode(a, b, c...);返回一個字符串,
該字符串每個字符的 ASCII 碼由 a, b, c... 等來確定。
indexOf() 用法:<字符串對象>.indexOf(<另一個字符串對象>[, <起始位置
>]);該方法從<字符串對象>中查找<另一個字符串對象>(如果給出<起始位置>
就忽略之前的位置),如果找到了,就返回它的位置,沒有找到就返回“-1”。
所有的“位置”都是從零開始的。
lastIndexOf() 用法:<字符串對象>.lastIndexOf(<另一個字符串對象>[, <起
始位置>]);跟 indexOf() 相似,不過是從后邊開始找。
split() 用法:<字符串對象>.split(<分隔符字符>);返回一個數組,該數組是
從<字符串對象>中分離開來的,<分隔符字符>決定了分離的地方,它本身不會包
含在所返回的數組中。例 如:' 1&2&345&678'.split('&')返回數組:1 ,2,345,678。
關于數組,我們等一下就討論。
substring() 用法:<字符串對象>.substring(<始>[, <終>]);返回原字符串的
子字符串,該字符串是原字符串從<始>位置到<終>位置的前一位置的一段。<終>
- <始> = 返回字符串的長度(length)。如果沒有指定<終>或指定得超過字符
串長度,則子字符串從<始>位置一直取到原字符串尾。如果所指定的位置不能返
回字符串,則返回空字符串。
substr() 用法:<字符串對象>.substr(<始>[, <長>]);返回原字符串的子字符
串,該字符串是原字符串從<始>位置開始,長度為<長>的一段。如果沒有指定<
長>或指定得超過字符串長度,則子字符串從<始>位置一直取到原字符串尾。如
果所指定的位置不能返回字符串,則返回空字符串。
toLowerCase() 用法:<字符串對象>.toLowerCase();返回把原字符串所有大寫
字母都變成小寫的字符串。
toUpperCase() 用法:<字符串對象>.toUpperCase();返回把原字符串所有小寫
字母都變成大寫的字符串。
Array 數組對象。數組對象是一個對象的集合,里邊的對象可以是不同類型的。
數組的每一個成員對象都有一個“下標”,用來表示它在數組中的位置(既然是
“位置”,就也是從零開始的啦)。
數組的定義方法:
var <數組名> = new Array();
這樣就定義了一個空數組。以后要添加數組元素,就用:
<數組名>[<下標>] = ...;
注意這里的方括號不是“可以省略”的意思,數 組的下標表示方法就是用方括號
括起來。
如果想在定義數組的時候直接初始化數據,請用:
var <數組名> = new Array(<元素1>, <元素 2>, <元素3>...);
例如,var myArray = new Array(1, 4.5, 'Hi'); 定義了一個數組 myArray,
里邊的元素是:myArray[0] == 1; myArray[1] == 4.5; myArray[2] == 'Hi'。
但是,如果元素列表中只有一個元素,而這個元素又是一個正整數的話,這將定
義一個包含<正整數>個空元素的數組。
注意:JavaScript 只有一維數組!千萬不要用“Array(3,4)”這種愚蠢的方法
來定義 4 x 5 的二維數組,或者用“myArray[2,3]”這種方法來返回“二維數
組”中的元素。任意“myArray[...,3]”這種形式的調用其實只返回了
“myArray[3]”。要使用多維數組,請用這種虛擬法:
var myArray = new Array(new Array(), new Array(), new Array(), ...);
其實這是一個一維數組,里邊的每一個元素又是一個數組。調用這個“二維數
組”的元素時:myArray[2][3] = ...;
屬性
length 用法:<數組對象>.length;返回:數組的長度,即數組里有多少個元素。
它等于數組里最后一個元素的下標加一。所以,想添加一個元素,只需要:
myArray[myArray.length] = ...。
方法
join() 用法:<數組對象>.join(<分隔符>);返回一個字符串,該字符串把數組
中的各個元素串起來,用<分隔符>置于元素與元素之間。這個方法不影響數組原
本的內容。
reverse() 用法:<數組對象>.reverse();使數組中的元素順序反過來。如果對
數組[1, 2, 3]使用這個方法,它將使數組變成:[3, 2, 1]。
slice() 用法:<數組對象>.slice(<始>[, <終>]);返回一個數組,該數組是原
數組的子集,始于<始>,終于<終>。如果不給出<終>,則子集一直取到原數組的
結尾。
sort() 用法:<數組對象>.sort([<方法函數>]);使數組中的元素按照一定的順
序排列。如果不指定<方法函數>,則按字母順序排列。在這種情況下,80 是比 9
排得前的。如果指定<方法函數>,則按<方法函數>所指定的排序方法排序。<方
法函數>比較難講述,這里只將一些有用的<方法函數>介紹給大家。
按升序排列數字:
function sortMethod(a, b) {
return a - b;
}
myArray.sort(sortMethod);
按降序排列數字:把上面的“a - b”該成“b - a”。
添加css樣式
<html>
<head>
<style>
.over{
color:red;
background:#888;
}
</style>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("div").bind("mouseover mouseout", function(){
$(this).toggleClass("over");
});
})
</script>
</head>
<body>
<div style="width:100px;height:50px;">滑入.</div>
</body>
</html>
根據事件類型
<html>
<head>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("div").bind("click.plugin",function(){
$("body").append("<p>click事件</p>");
});
$("div").bind("mouseover.plugin", function(){
$("body").append("<p>mouseover事件</p>");
});
$("div").bind("dblclick", function(){
$("body").append("<p>dblclick事件</p>");
});
$("button").click(function() {
$("div").unbind(".plugin");
})
})
</script>
</head>
<body>
<div style="width:100px;height:50px;background:#888;color:white;">test.</div>
<button >根據命名空間,刪除事件</button>
</body>
</html>
相同事件名稱,不同命名空間執行方法
<html>
<head>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("div").bind("click",function(){
$("body").append("<p>click事件</p>");
});
$("div").bind("click.plugin", function(){
$("body").append("<p>click.plugin事件</p>");
});
$("button").click(function() {
$("div").trigger("click!"); // 注意click后面的感嘆號
});
})
</script>
</head>
<body>
<div style="width:100px;height:50px;background:#888;color:white;">test.</div>
<button >根據命名空間,觸發事件</button>
</body>
</html>
<html>
<head>
<style type="text/css">
*{margin:0;padding:0;}
body { font-size: 13px; line-height: 130%; padding: 60px; }
p {width:200px;background:#888;color:white;height:16px;}
</style>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$('#btn').bind("myClick", function(){
$('#test').append("<p>我的自定義事件.</p>");
});
$('#btn').click(function(){
$(this).trigger("myClick");
}).trigger("myClick");
})
</script>
</head>
<body>
<button id="btn">點擊我</button>
<div id="test"></div>
</body>
</html>
<html>
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
body { font-size: 13px; line-height: 130%; padding: 60px; }
p {width:200px;background:#888;color:white;height:16px;}
</style>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$('#old').bind("click", function(){
$("input").trigger("focus");
});
$('#new').bind("click", function(){
$("input").triggerHandler("focus");
});
$("input").focus(function(){
$("body").append("<p>focus.</p>");
})
})
</script>
</head>
<body>
<button id="old">trigger</button>
<button id="new">triggerHandler</button>
<input />
</body>
</html>
<html>
<head>
<style type="text/css">
*{margin:0;padding:0;}
body { font-size: 13px; line-height: 130%; padding: 60px; }
p {width:200px;background:#888;color:white;height:16px;}
</style>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$('#btn').bind("click", function(){
$('#test').append("<p>我的綁定函數1</p>");
}).bind("click", function(){
$('#test').append("<p>我的綁定函數2</p>");
}).bind("click", function(){
$('#test').append("<p>我的綁定函數3</p>");
});
$('#delAll').click(function(){
$('#btn').unbind("click");
});
$('#delTwo').click(function(){
$('#btn').unbind("click",myFun2);
});
})
</script>
</head>
<body>
<button id="btn">點擊我</button>
<div id="test"></div>
<button id="delAll">刪除所有事件</button>
<button id="delTwo">刪除第二個事件</button>
</body>
</html>
<html>
<head>
<style type="text/css">
*{margin:0;padding:0;}
body { font-size: 13px; line-height: 130%; padding: 60px; }
p {width:200px;background:#888;color:white;height:16px;}
</style>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$('#btn').one("click", function(){
$('#test').append("<p>我的綁定函數1</p>");
}).one("click", function(){
$('#test').append("<p>我的綁定函數2</p>");
}).one("click", function(){
$('#test').append("<p>我的綁定函數3</p>");
});
})
</script>
</head>
<body>
<button id="btn">點擊我</button>
<div id="test"></div>
</body>
</html>
<html>
<head>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script>
$(function(){
$("a").click(function(event) {
alert(event.type);//獲取事件類型
alert(event.target.href);//獲取觸發事件的<a>元素的href屬性值
alert("Current mouse position: " + event.pageX + ", " + event.pageY );//獲取鼠標當前相對于頁面的坐標
alert(e.which) // 1 = 鼠標左鍵 left; 2 = 鼠標中鍵; 3 = 鼠標右鍵
return false;//阻止鏈接跳轉
});
})
</script>
</head>
<body>
<a >click me .</a>
</body>
</html>
<html>
<head>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#sub").bind("click",function(event){
var username = $("#username").val(); //獲取元素的值
if(username==""){ //判斷值是否為空
$("#msg").html("<p>文本框的值不能為空.</p>"); //提示信息
event.preventDefault(); //阻止默認行為 ( 表單提交 )或者return false;
}
})
})
</script>
</head>
<body>
<form action="test.html">
用戶名:<input type="text" id="username" />
<br/>
<input type="submit" value="提交" id="sub"/>
</form>
<div id="msg"></div>
</body>
</html>
<html>
<head>
<style type="text/css">
*{margin:0;padding:0;}
body { font-size: 13px; line-height: 130%; padding: 60px; }
#content { width: 220px; border: 1px solid #0050D0;background: #96E555 }
span { width: 200px; margin: 10px; background: #666666; cursor: pointer;color:white;display:block;}
p {width:200px;background:#888;color:white;height:16px;}
</style>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
// 為span元素綁定click事件
$('span').bind("click",function(event){
var txt = $('#msg').html() + "<p>內層span元素被點擊.<p/>";
$('#msg').html(txt);
event.stopPropagation(); // 阻止事件冒泡或者return false;
});
// 為div元素綁定click事件
$('#content').bind("click",function(event){
var txt = $('#msg').html() + "<p>外層div元素被點擊.<p/>";
$('#msg').html(txt);
event.stopPropagation(); // 阻止事件冒泡或者return false;
});
// 為body元素綁定click事件
$("body").bind("click",function(){
var txt = $('#msg').html() + "<p>body元素被點擊.<p/>";
$('#msg').html(txt);
});
})
</script>
</head>
<body>
<div id="content">
外層div元素
<span>內層span元素</span>
外層div元素
</div>
<div id="msg"></div>
</body>
</html>
<html>
<head>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#panel h5.head").hover(function(){//鼠標移動事件
$(this).next().show();
},function(){
$(this).next().hide();
})
})
</script>
</head>
<body>
<div id="panel">
<h5 class="head">什么是jQuery?</h5>
<div class="content">
jQuery是繼Prototype之后又一個優秀的JavaScript庫,它是一個由 John Resig 創建于2006年1月的開源項目。jQuery憑借簡潔的語法和
跨平臺的兼容性,極大地簡化了JavaScript開發人員遍歷HTML文檔、操作DOM、處理事件、執行動畫和開發Ajax。它獨特而又優雅的代碼風格改變了
JavaScript程序員的設計思路和編寫程序的方式。
</div>
</div>
</body>
</html>
<html>
<head>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<style type="text/css">
.highlight{ background:#FF3300; }
</style>
<script type="text/javascript">
$(function(){
$("#panel h5.head").toggle(function(){//鼠標點擊事件
$(this).addClass("highlight");
$(this).next().show();
},function(){
$(this).removeClass("highlight");
$(this).next().hide();
});
})
</script>
</head>
<body>
<div id="panel">
<h5 class="head">什么是jQuery?</h5>
<div class="content">
jQuery是繼Prototype之后又一個優秀的JavaScript庫,它是一個由 John Resig 創建于2006年1月的開源項目。jQuery憑借簡潔的語法和
跨平臺的兼容性,極大地簡化了JavaScript開發人員遍歷HTML文檔、操作DOM、處理事件、執行動畫和開發Ajax。它獨特而又優雅的代碼風格改變了
JavaScript程序員的設計思路和編寫程序的方式。
</div>
</div>
</body>
</html>
<head>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#panel h5.head").bind("click",function(){
var $content = $(this).next();
if($content.is(":visible")){
$content.hide();
}else{
$content.show();
}
})
})
</script>
</head>
<body>
<div id="panel">
<h5 class="head">什么是jQuery?</h5>
<div class="content">
jQuery是繼Prototype之后又一個優秀的JavaScript庫,它是一個由 John Resig 創建于2006年1月的開源項目。jQuery憑借簡潔的語法和
跨平臺的兼容性,極大地簡化了JavaScript開發人員遍歷HTML文檔、操作DOM、處理事件、執行動畫和開發Ajax。它獨特而又優雅的代碼風格改變了
JavaScript程序員的設計思路和編寫程序的方式。
</div>
</div>
</body>
</html>
<html >
<head>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#panel h5.head").bind("mouseover",function(){
$(this).next().show();
});
$("#panel h5.head").bind("mouseout",function(){
$(this).next().hide();
})
})
</script>
</head>
<body>
<div id="panel">
<h5 class="head">什么是jQuery?</h5>
<div class="content">
jQuery是繼Prototype之后又一個優秀的JavaScript庫,它是一個由 John Resig 創建于2006年1月的開源項目。jQuery憑借簡潔的語法和
跨平臺的兼容性,極大地簡化了JavaScript開發人員遍歷HTML文檔、操作DOM、處理事件、執行動畫和開發Ajax。它獨特而又優雅的代碼風格改變了
JavaScript程序員的設計思路和編寫程序的方式。
</div>
</div>
</body>
</html>
<html>
<head>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#panel h5.head").mouseover(function(){
$(this).next().show();
});
$("#panel h5.head").mouseout(function(){
$(this).next().hide();
})
})
</script>
</head>
<body>
<div id="panel">
<h5 class="head">什么是jQuery?</h5>
<div class="content">
jQuery是繼Prototype之后又一個優秀的JavaScript庫,它是一個由 John Resig 創建于2006年1月的開源項目。jQuery憑借簡潔的語法和
跨平臺的兼容性,極大地簡化了JavaScript開發人員遍歷HTML文檔、操作DOM、處理事件、執行動畫和開發Ajax。它獨特而又優雅的代碼風格改變了
JavaScript程序員的設計思路和編寫程序的方式。
</div>
</div>
</body>
</html>
<head>
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
var startTime = new Date().getTime();
$(document).ready(function(){
test1();
})
function test1(){
var endTime2 = new Date().getTime();
var a = endTime2 - startTime;
$("<div>jQuery的ready() : "+a+" ms</div>").appendTo("body");
}
function test2(){
var endTime1 = new Date().getTime();
var b = endTime1 - startTime;
$("<p>JavaScript的window.onload : "+b+" ms</p>").appendTo("body");
}
</script>
</head>
<body onload="test2();">
<img src="demo.jpg" style="width:200px;height:200px;"/>
</body>
</html>