使用Jquery查看對象註冊事件

來源:互聯網
上載者:User

標籤:

How To Find Events Bound To An Element With jQuery

28th May 2013

Share your passion with the world. Create a beautiful website today.ads via Carbon

In this tutorial we are going to investigate ways you can tell what events a certain element has bound to it. This is really useful when you need to debug the on method to see how it is working.

There are a number of ways to bind events to an element, there is bind(), live() anddelegate().

  • Bind - Used to attach events to current elements on the page. If new elements are added with the same selector the event will not be added to the element.
  • Live - This is used to attach an events to all elements on the page and future elements with the same selector.
  • Delegate - This is used to attach events to all elements on the page and future elements based on a specific root element.

As of jQuery 1.7 the on method will give you all the functionality that you need to bound events to elements. It will allow you to add an event to all elements on the page and all future elements.

$(‘.element‘).on(‘click‘, function(){  // stuff});$(‘.element‘).on(‘hover‘, function(){  // stuff});$(‘.element‘).on(‘mouseenter‘, function(){  // stuff});$(‘form‘).on(‘submit‘, function(){  // stuff});

When you are assigning events to future elements you might want to find out what events are currently assigned to these elements.

Using developer tools in your browser you can see what events are currently bound to the element by using these 2 methods. First you can display the events by using the console, the second method is to view the events in the event listener window in the developer tools.

Display Events In Console

In your browser if you press F12 a new window called developer tools will open, this allows you to view the entire HTML DOM in more detail, this also has a console feature which will allow you to type in any Javascript to run at this current time on the page.

You can use the console to display a list of events currently assigned to an element by using the following code.

$._data( $(‘.element‘)[0], ‘events‘ );

This will display all the events that are currently assigned to the element.

Event Listener Window

The other option to use to view what events are currently assigned to an element is to use the event listener window in your browser‘s developer tools. All you have to do is press F12 to open the developer tools, select the element in the HTML DOM that you want to investigate, on the right side of the window you will see an option called Event Listeners.

When you expand this menu you will see all the current events assigned to the element including all click events bound from jQuery.

使用Jquery查看對象註冊事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.