jQuery 常用的方法整理如下
1.jquery data(name)
data() 方法向被選元素附加資料,或者從被選元素擷取資料。
$("#btn1").click(function(){
$("div").data("greeting", "Hello World");
});
$("#btn2").click(function(){
alert($("div").data("greeting"));
});
2.jquery arguments
arguments是指向實參對象的引用,實參對象是一個類數組對象 .
arguments 的索引 從 0, 1,2,....遞增,與實參逐個對應.
arguments.length 屬性代表實參的個數
arguments一定不是數組, 是長的比較像數組的一個對象,雖然也有length屬性
arguments每個函數都會有,因此,arguemnts只會在內部找自身的arguments,無法引用到外層的arguments
// 求圓形面積,矩形面積, 三角形面積
function area () {
if(arguments.length == 1) {
alert(3.14 * arguments[0] * arguments[0]);
} else if(arguments.length == 2) {
alert(arguments[0] * arguments[1]);
} else if(arguments.length == 3) {
alert(arguments[0] + arguments[1] + arguments[2]);
} else {
return null;
}
}
//調用
area(10,20,30);
3.jquery target() event.target
target 屬性規定哪個 DOM 元素觸發了該事件。
$("p, button, h1, h2").click(function(event){
$("div").html("Triggered by a " + event.target.nodeName + " element.");
});
<p></p>
<button></button>
<h1></h1>
<h2></h2>
//當點擊p標籤的時候顯示:點擊事件由 P 元素觸發
....
4.jquery trigger(event,[參1,參2,...])
trigger() 方法觸發被選元素的指定事件類型。(可自訂事件,可以傳參)自訂事件非常重要且有用!
//myEvent為自訂事件名
$("#p1").bind("myEvent",function(str1,str2) {
alert(str1 + ' ' + str2);
});
$("#p1").trigger("myEvent",["Hello","World"]);
//也可以這樣寫:
$("#p1").bind("myEvent",function(str1,str2) {
alert(str1 + ' ' + str2);
}).trigger("myEvent",["Hello","World"]);
5.js substring(start,stop)
substring() 方法用於提取字串中介於兩個指定下標之間的字元。
substring() 方法返回的子串包括 start 處的字元,但不包括 stop 處的字元。
var str="Helloworld!"
document.write(str.substring(3,7))
//結果
lowo
var str="Hello world!"//有兩個Null 字元
document.write(str.substring(3,7))
//結果
lo
//兩者的結果有區別,字串之間的Null 字元串佔用索引!
看清楚 沒有r 這個stop處的字元!
重要事項:與 slice() 和 substr() 方法不同的是,substring() 不接受負的參數。
6.js slice(start,stop)
slice() 方法可提取字串的某個部分,並以新的字串返回被提取的部分。與上面的substring一樣不包括 stop處的字元;
還有一個不同是:start,stop可以使用負數!也就是說,-1 指字串的最後一個字元,-2 指倒數第二個字元,以此類推。
71.siblings()
siblings() 獲得匹配集合中每個元素的同胞,通過選取器進行篩選是可選的。
<body>
<div><span>Hello</span></div>
<p class="selected">Hello Again</p>
<p>And Again</p>
<script>
$("p").siblings(".selected").css("background", "yellow");
</script>
</body>
結果:
8.nextAll()
nextAll() 方法返回被選元素的所有跟隨的同胞元素。
<script>
$(document).ready(function(){
$("h2").nextAll().css({"color":"red","border":"2px solid red"});
});
</script>
</head>
<body class="siblings">
<div>div (父)
<p>p</p>
<span>span</span>
<h2>h2</h2>
<h3>h3</h3>
<p>p</p>
</div>
</body>
結果:
9.end()
end() 方法結束當前鏈條中的最近的篩選操作,並將匹配元素集還原為之前的狀態。
<head>
<style>p { margin:10px; padding:10px; }</style>
<script type="text/javascript" src="/jquery/jquery.js"></script>
</head>
<body>
<p><span>Hello</span>, how are you?</p>
<script>$("p").find("span").end().css("border", "2px red solid");</script>
</body>
結果:
10.wrap(wrapper)
wrap() 方法把每個被選元素放置在指定的 HTML 內容或元素中。
<script type="text/javascript">
$(document).ready(function(){
$(".btn1").click(function(){
$("p").wrap("<div></div>");
});
});
</script>
<style type="text/css">
div{background-color:yellow;}
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button class="btn1">用 div 包裹每個段落</button>
</body>
結果:
wrap(function())
使用函數來規定在每個被選元素周圍包裹的內容。
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").wrap(function(){
return "<div></div>"
});
});
});
</script>
<style type="text/css">
div{background-color:yellow;padding:10px;}
</style>
</head>
<body>
<p>這是一個段落。</p>
<button>用 div 元素來包圍 p 元素</button>
</body>
結果:
11.each()
each() 方法規定為每個匹配元素規定啟動並執行函數。(提示:返回 false 可用於及早停止迴圈。)
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("li").each(function(){
alert($(this).text())
});
});
});
</script>
</head>
<body>
<button>輸出每個清單項目的值</button>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>
</body>
結果:
12.call()
調用一個對象的一個方法,以另一個對象替換當前對象。
apply()
應用某一對象的一個方法,用另一個對象替換當前對象。
提示:call與apply的不同就是call傳的值可以是任意的,而apply傳的剩餘值必須為數組。
function add(a, b) { return a + b; }
function sub(a, b) { return a - b; }
//apply
var a1 = sub.apply(add, [4, 2]);
var a2= add.apply(sub, [4, 2]);
//call
var a1 = sub.call(add, 4, 2);
var a2= add.call(sub, 4, 2);
//輸出:a1=2 a2=6