事件绑定的方法有两种:javascript
方法1:绑定到元素html
<body>
<p onclick='func1()'>点击我</p>
</body>
<script type="text/javascript">
function func1(){
alert('123');
}
方法2:查找到元素后绑定事件java
<body>
<p>点击我</p>
</body>
<script type="text/javascript">
$('p').click(function(){
alert('123');
});
</script>
说明:方法2的优势是不用在元素里面进行事件添加,至关于事件和元素分离。jquery
resize()//只要在浏览器窗口的大小改变时,根据不一样的浏览器,该消息被追加到<div id="log">一次或屡次。浏览器
<body>
<p id='log'>点击我</p>
</body>
<script type="text/javascript">
$(window).resize(function() {
$('#log').append('<div>Handler for .resize() called.</div>');
});
</script>
二、scroll()//当用户在元素内执行了滚动操做,就会在这个元素上触发scroll
事件。app
$('#target').scroll(function() {
$('#log').append('<div>Handler for .scroll() called.</div>');
});
当DOM准备就绪时,执行的一个函数。ide
$( document ).ready(function() {
// Handler for .ready() called.
});
$(function() {
// Handler for .ready() called.
});
bind(事件名称,函数)和unbind(‘click’,function(){})函数
$('#foo').bind('click', function() {
alert('User clicked on "foo."');
});
<!DOCTYPE html> <html> <head> <style> button { margin:5px; } button#theone { color:red; background:yellow; } </style> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <button id="theone">Does nothing...</button> <button id="bind">Add Click</button> <button id="unbind">Remove Click</button> <div style="display:none;">Click!</div>
<script>
//定义函数aClick,然div显示,并展现慢慢消失效果。 function aClick() { $("div").show().fadeOut("slow"); }
//绑定点击函数,针对#theone绑定aClick函数,并把内容改成‘Can Click!’ $("#bind").click(function () { $("body").on("click", "#theone", aClick) .find("#theone").text("Can Click!"); });
//找到unbind元素并绑定click事件,执行aClick函数,找到#theone元素而后把内容改成‘Does nothing.....’ $("#unbind").click(function () { $("body").off("click", "#theone", aClick) .find("#theone").text("Does nothing..."); }); </script> </body> </html>
<input>
元素,<textarea>
和<select>
元素。<input>
, <select>
等)和连接元素(<a href>
)。<input type="text">
和<textarea>
。<form>
元素上。如下几种状况会致使表单被提交:用户点击了<input type="submit">
, <input type="image">
, 或者 <button type="submit">
,或者当某些表单元素获取焦点时,敲击Enter(回车键),均可以提交。