使用jQuery的onclick和onclick属性有什么区别? [英] What's the difference between using jQuery's onclick and the onclick attribute?

查看:141
本文介绍了使用jQuery的onclick和onclick属性有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两段HTML有什么区别(很抱歉,如果我在徒手输入时输入任何错字)?

What's the difference between the following two pieces of HTML (apologies if there are any typos as I'm typing this freehand)?

使用jQuery:

<script type="text/javascript">
    $(document).ready(function() {
        $("#clickme").click(function() {
            alert("clicked!");
        });
    });
</script>

<a id="clickme" href="javascript:void(0);">click me</a>

不使用jQuery:

<a id="clickme" href="javascript:void(0);" onclick="alert('clicked!');">click me</a>

推荐答案

一个很大的不同是jQuery的事件是在注册表中处理的,而注册表是在click事件中解析的.至关重要的是,这意味着您可以分配多个回调,并按照注册的顺序触发它们:

One big difference is that jQuery's events are handled in a registry which is parsed on the click event. Crucially, this means that you are permitted to assign multiple callbacks, and have them triggered in the order in which they were registered:

<script type="text/javascript">
    $(document).ready(function() {
        $("#clickme").click(function() {
            alert("clicked!");
        });
        $("#clickme").click(function() {
            alert("I concur, clicked!");
        });
    });
</script>

它们都将在click事件上以该顺序被调用. jQuery的注册表驱动系统将覆盖真实的" onClick事件.在原始文档结构中,如果没有jQuery之类的系统,则只能有一个onClick事件.

They will both be invoked on the click event, in that order. The "real" onClick event is overridden by jQuery's registry-driven system. In the vanilla document structure, without a system like jQuery in place, there can only be one onClick event.

这篇关于使用jQuery的onclick和onclick属性有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆