实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<script src="https://libs.baidu.com/jquery/1.10.2/jquery.min.js">
5
</script>
6
<script>
7
$(document).ready(function(){
8
  $("#div1").on("click",function(){
9
    $(this).css("background-color","pink");
10
  });
11
  $("#div2").bind("click",function(){
12
    $(this).css("background-color","pink");
13
  });
14
});
15
</script>
16
</head>
17
<body>
18
19
<h4 style="color:green;">This example demonstrates how to achieve the same effect using on() and bind().</h4>
20
21
<div id="div1" style="border:1px solid black;">This is some text.
22
<p>Click to set background color using the <b>on() method</b>.</p>
23
</div><br>
24
25
<div id="div2" style="border:1px solid black;">This is some text.
26
<p>Click to set background color using the <b>bind() method</b>.</p>
27
</div>
28
29
</body>
30
</html>