jQuery 本身即是为事件处理而特别设计的,jQuery 事件处理方法是 jQuery 中的核心函数。
$(function() { ... }); 是如下格式的缩写:
|
1
|
$(document).ready(function() { ... });
|
0. $ 符号
根据类别,定位标签:
|
1
2
3
4
5
6
|
<button>click me</button><p>hide me</p><p>hide me 2</p>$(\'button\').click(function(){
$(\'p\').hide(); // 作用在两个 <p>/</p> 上
}); |
根据 id 定位标签:
|
1
2
|
<h2 id=\'temp\'></p>
$(\'#temp\').html(...);
|
1. demo:点击按钮,将段落折叠
|
1
2
3
4
5
|
$(document).ready(function () {
$(\'button\').click(function(){
$(\'p\').hide();
});
}); |
2. jQuery ajax - getJSON() 方法
使用 AJAX 请求来获得 JSON 数据,并输出结果:
|
1
2
3
4
5
6
7
|
$("button").click(function(){
$.getJSON("demo_ajax_json.js",function(result){
$.each(result, function(i, field){ // 遍历数组
$("div").append(field + " ");
});
});
});
|