


Query中.live()方法的使用方法
今天在写代码的时候遇到一个问题,直接上代码看:
$(function(){
$(".file").live("click",function(){
var task_name=$(this).text();
$("#selecting tbody").append("
});
});
$(".file")对象是从后台传过来的,click肯定是不行的,bind()也无法获取动态添加的元素,因此只能用live(),但是使用live()遇到的问题是表格行会莫名其妙的添加了两行,即绑定事件重复执行了,纠结了一上午终于找到了原因,先看live()方法的介绍。
live(type, [data],fn)
概述
jQuery给所有匹配的元素附加一个事件处理函数,即使这个元素是以后再添加进来的也有效。
这个方法是基本是的 .bind() 方法的一个变体。使用 .bind()时,选择器匹配的元素会附加一个事件处理函数,而以后再添加的元素则不会有。为此需要再使用一次 .bind() 才行。比如说
可以给这个元素绑定一个简单的click事件:
$('.clickme').bind('click', function() {
alert("Bound handler called.");
});
当点击了元素,就会弹出一个警告框。
然后,想象一下这之后有另一个元素添加进来了。
$('body').append('
尽管这个新的元素也能够匹配选择器".clickme" ,但是由于这个元素是在调用 .bind() 之后添加的,所以点击这个元素不会有任何效果。
.live()就提供了对应这种情况的方法。如果我们是这样绑定click事件的:
$('.clickme').live('click', function() {
alert("Live handler called.");
});
然后再添加一个新元素:
$('body').append('
Then click on the newly added element, he can still trigger the event handler function.
Event Delegate
The.live() method works on an element that has not yet been added to the DOM due to the use of event delegation: event handlers bound to ancestor elements 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 occur:
1. Generate a click event and pass it to 2. Since there is no event handling function directly bound to 3. Events continue to bubble up to the root node of the DOM tree. This special event handler is bound to it by default. 4. Execute the special click event processing function bound by .live(). 5. This event processing 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. 6. If a matching element is found, call the original event handling function. Since the test in step 5 above is only done when the event occurs, elements added at any time can respond to this event. .live() is useful, but due to its special implementation, it cannot simply replace .bind() in any situation. The main differences are: In jQuery 1.4, the .live() method supports custom events and all JavaScript events. In jQuery 1.4.1, focus and blue events are even supported (mapped to the more appropriate and bubbling focusin and focusout). In addition, in jQuery1.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 processing function is bound with .live(), and you want to stop executing other event processing functions, this function must return false. Simply calling .stopPropagation() cannot achieve this purpose. In jQuery 1.4.1, you can bind multiple events to .live() at one time, similar to the functionality provided by .bind(). 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. typeString Event type data (optional) Object The event handler function to be bound fn Function The event processing function to be bound HTML code: Clickme! jQuery code: Anotherparagraph!< ;/p>"); Prevent default event behavior and event bubbling, return false jQuery code: //The root cause is here, you need to prevent the default event behavior and event bubbling, add return false; after the code, it will be OK Description: Only prevent default event behavior jQuery code:
Additional Notes
Refer to the .bind() method for more information about event binding.
Parameters
Example
$("p").live("click", function(){
$(this).after("
});
Description:
$("a").live("click",function() { return false; });
$("a").live("click", function(event){
event.preventDefault();
});

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
