jQuery入门[3]-事件

2016-02-19 13:54 1 1 收藏

关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享jQuery入门[3]-事件教程,希望对大家能有一点小小的帮助。

【 tulaoshi.com - Web开发 】

jQuery对事件的支持主要包括:
bind()--为事件绑定处理程序,如:
$("p").bind("mouseenter mouseleave", function(e){
$(this).toggleClass("over");
});
unbind()--注销绑定在事件上的处理程序,如:$(document).unbind('ready');,如不给参数,则清除所有事件处理程序。
$("#unbind").click(function () {
$("#theone").unbind('click', aClick);
});
trigger()--触发某类事件。
$("button:first").trigger('click');
triggerHandler()--触发某类事件,但不触发默认的事件处理逻辑,比如a的定向。
$("input").triggerHandler("focus");
one()--为事件绑定只能被触发一次的处理程序。
$("div").one("click", function(){
});
ready()/click()/change()/toggle(fn,fn)/dblclick()各种常规事件的快捷方式,xxx(fn)为绑定处理程序,xxx()为触发事件

jQuery 1.2的事件支持命名空间,
  $("div").bind("click", function(){ alert("hello"); });
$("div").bind("click.plugin", function(){ alert("goodbye"); });
$("div").trigger("click!"); // alert("hello") only

DEMO:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

html xmlns="http://www.w3.org/1999/xhtml"
head
    titleEvents/title
    script src="../scripts/jquery-1.2.3.intellisense.js" type="text/javascript"/script
    style type="text/css"
        textarea
        {
            height: 118px;
            width: 280px;
        }
    /style
    script type="text/javascript"
        $(function(){
            $('textarea').bind('propertychange',function(){
                $('#result').html($('textarea').val())
            }
            ).bind('change',function(){
                alert($('textarea').val());
            });
        });
    
    /script
/head
body
    textarea/textarea
    div id='result'/div
/body
/html
运行效果如下:


Reference:http://docs.jquery.com/Events

来源:https://www.tulaoshi.com/n/20160219/1605587.html

延伸阅读
标签: Web开发
大家都发现,通过jQuery绑定事件是件非常容易的事情 代码如下: TEXTAREA class=javascript name=code rows=15 cols=50$("a").click(function(){ console.info("A"); return false; }); /TEXTAREA 但是A事件绑定后,我发现我需要B事件来决定其是否触发,好办,现在就改。 代码如下: TEXTAREA class=javascript name=code rows=15 c...
标签: Web开发
前台 代码如下: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://www.w3.org/1999/xhtml" head title无标题页/title style type="text/css" .show{ display:block;} .hide{ display:none;} /style script type="text/javascript" s...
标签: Web开发
jQuery另一个很令人惬意的地方是,一般的代码都是一行一行写,jQuery的代码可以一串一串写。 这一点,在前面的文章中已经介绍过了。 直接来一个Demo: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0&nbs...
标签: Web开发
目前支持 click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup。 还不支持 blur, focus, mouseenter, mouseleave, change, submit 与bind()不同的是,live()一次只能绑定一个事件。 这个方法跟传统的bind很像,区别在于用live来绑定事件会给所有当前以及将来在页面上的元素绑定事件(使用委派的...
标签: Web开发
JQuery优点 ◦体积小(v1.2.3 15kb) ◦丰富的DOM选择器(CSS1-3 + XPath) ◦跨浏览器(IE6,FF,Safari,Opera) ◦链式代码 ◦强大的事件、样式支持 ◦强大的AJAX功能 ◦易于扩展,插件丰富 jQuery的构造函数接收四种类型的参数: jQuery(expression,context) jQuery(html) jQuery(elements) jQue...