Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the use of jQuery live event

黄舟
Release: 2017-06-26 09:16:12
Original
1397 people have browsed it

Overview

jQuery attaches an event handling function to all matching elements, even if the element is added later.

This method is basically a variant of the .bind() method. When using .bind(), elements matched by the selector will have an event handler attached to them, while elements added later will not. You need to use .bind() again for this. For example,

<body>
  <div class="clickme">Click here</div>
</body>
Copy after login

can bind a simple click event to this element:

$(&#39;.clickme&#39;).bind(&#39;click&#39;, function() {
  alert("Bound handler called.");
});
Copy after login

When the element is clicked, a warning box will pop up. Then, imagine that another element is added after this.

$(&#39;body&#39;).append(&#39;<div class="clickme">Another target</div>&#39;);
Copy after login
Copy after login

Although this new element can also match the selector ".clickme", since this element is added after calling .bind(), clicking on this element will have no effect.

.live() provides a method for this situation. If we bind the click event like this:

$(&#39;.clickme&#39;).live(&#39;click&#39;, function() {
  alert("Live handler called."); 
});
Copy after login

and then add a new element:

$(&#39;body&#39;).append(&#39;<div class="clickme">Another target</div>&#39;);
Copy after login
Copy after login

Then click on the new element, he can still trigger the event handler.

Event Delegation

The.live() method can be effective for an element that has not been added to the DOM due to the use of event delegation: the event handling function bound to the ancestor element can Respond to events triggered on descendants. The event handler passed to .live() will not be bound to the element, but will be treated as a special event handler and bound to the root node of the DOM tree. In our example, when a new element is clicked, the following steps will occur:

Generate a click event and pass it to

for processing

Since there is no event handling function Directly bound to

Events continue to bubble up to the root node of the DOM tree. This special event handler is bound to it by default.

Execute the special click event handling function bound by .live().

This event handling function first detects the target of the event object to determine whether it needs to continue. This test is implemented by checking whether $(event.target).closest('.clickme') can find a matching element.

If a matching element is found, the original event handler is called.

Since the test in step 5 above will only be done when the event occurs, elements added at any time can respond to this event.

Additional Notes

Although .live() is useful, due to its special implementation, it cannot simply replace .bind() in any case. The main differences are:

In jQuery 1.4, the .live() method supports custom events and also supports all JavaScript events. In jQuery 1.4.1, there is even support for focus and blue events (mapped to the more appropriate, bubbling focusin and focusout). In addition, in jQuery 1.4.1, hover (mapped to "mouseenter mouseleave") is also supported. However, in jQuery 1.3.x, only supported JavaScript events and custom events are supported: click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup.

.live() does not fully support elements found through DOM traversal. Instead, you should always use the .live() method directly after a selector, as mentioned in the previous example.

When an event handling function is bound with .live(), to stop executing other event handling functions, this function must return false. Simply calling .stopPropagation() will not accomplish this.

Refer to the .bind() method for more information about event binding.

In jQuery 1.4.1, .live() can accept multiple events, separate events with spaces, and provide functions similar to .bind(). For example, we can "live" bind both the mouseover and mouseout events, like this:

$(&#39;.hoverme&#39;).live(&#39;mouseover mouseout&#39;, function(event) {
  if (event.type == &#39;mouseover&#39;) {
    // do something on mouseover
  } else {
    // do something on mouseout
  }
});
Copy after login

In jQuery 1.4.3: You can bind one or more event types to StringAnd function data mapping to execute them

$("a").live({
  click: function() {
    // do something on click
  },
  mouseover: function() {
    // do something on mouseover
  }
});
Copy after login

In jQuery 1.4, the data parameter can be used to pass additional information to the event handler function. A good use is to deal with problems caused by closures. See the discussion of .bind() for more information.

In jQuery 1.4, the live event can be bound to the "context" DOM ​​element instead of the default document root. To set this context, we use the jQuery() function's second argument on a single DOM element (rather than a jQuery collection or selector).

$(&#39;div.clickme&#39;, $(&#39;#container&#39;)[0]).live(&#39;click&#39;, function() {
  // Live handler called.
});
Copy after login

参数

type,[fn]String,FunctionV1.3

type:一个或多个事件类型,由空格分隔多个事件。

fn:要从每个匹配元素的事件中反绑定的事件处理函数

type,[data],falseString,Array,boolV1.4

type:一个或多个事件类型,由空格分隔多个事件。

data:传递给事件处理函数的附加参数

false:设置为false会使默认的动作失效。

eventStringV1.4.3

一个或多个事件类型的字符串和函数的数据映射来执行他们

示例

描述:

点击生成的p依然据有同样的功能。

HTML 代码:

<p>Click me!</p>
Copy after login

jQuery 代码:

$("p").live("click", function(){ $(this).after("<p>Another paragraph!</p>"); });
Copy after login

描述:

阻止默认事件行为和事件冒泡,返回 false

jQuery 代码:

$("a").live("click", function() { return false; });
Copy after login

描述:

仅仅阻止默认事件行为

jQuery 代码:

$("a").live("click", function(event){ event.preventDefault(); });
Copy after login

这是代码,用live绑定的事件不执行,但是创建的元素能添加进去。这是为什么

Detailed explanation of the use of jQuery live event

你的_html是字符串啊,你是不是要点击li触发事件?
那应该是

$(&#39;li&#39;).live(&#39;click&#39;,function(){....})
Copy after login

The above is the detailed content of Detailed explanation of the use of jQuery live event. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!