Home > Web Front-end > JS Tutorial > Summary of common jquery functions and methods_jquery

Summary of common jquery functions and methods_jquery

WBOY
Release: 2016-05-16 15:41:15
Original
1124 people have browsed it

1.delay(duration,[queueName])

Set a delay to delay execution of subsequent items in the queue.

New in jQuery 1.4. Used to delay the execution of functions in the queue. It can either delay the execution of the animation queue or be used in a custom queue.

duration: delay time, unit: milliseconds

queueName: queue noun, the default is Fx, animation queue.

Example:

Head and bottom delayed loading animation effect

$(document).ready(function() {
  $('#header') .css({ 'top':-50 }) .delay(1000).animate({'top': 0}, 800);
  $('#footer') .css({ 'bottom':-15 }) .delay(1000).animate({'bottom': 0}, 800); 
});
Copy after login

2.jQuery live(type, fn) delegated event implementation

New method in Query 1.3. Bind an event handler (such as click event) to all current and future matching elements. Custom events can also be bound.

Currently supports click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup.

Blur, focus, mouseenter, mouseleave, change, submit

are not supported yet

Different from bind(), live() can only bind one event at a time.

This method is very similar to traditional bind. The difference is that using live to bind events will bind events to all current and future elements on the page (using delegation). For example, if you use live to bind click events to all li on the page. Then when a li is added to this page in the future, the click event of this newly added li is still available. There is no need to re-bind events to this newly added element.

.live() is very similar to the popular liveQuery plugin, but has the following major differences:

.live currently only supports a subset of all events. Please refer to the description above for the supported list.

.live does not support the "eventless" style callback function provided by liveQuery. .live can only bind event handling functions.

.live does not have "setup" and "cleanup" processes. Because all events are delegated rather than directly bound to elements.

To remove events bound with live, please use the die method

Usage example:

<div class=”myDiv”></div>
Copy after login

jquery:

$(“.myDiv”).live(“click”, function(){
alert(“clicked!”);
});
Copy after login

If you use javascript to dynamically create an element with class mydiv, a pop-up will still appear when you click on the element. Why does it happen after using live? This is because jquery uses the event bubbling mechanism to directly bind the event to the document, and then finds the source of the event through event.target. This is different from the jquery.livequery plug-in. jquery.livequery checks every 20 milliseconds and rebinds the event if there is a new one.

Of course there are advantages and disadvantages to using live:

The advantage is: there is no need to repeatedly define events when elements are updated.

The disadvantage is: binding the event to the document will call it once for every element on the page. If used improperly, it will seriously affect performance.

And it does not support blur, focus, mouseenter, mouseleave, change, submit.

2. Remove live-bound events

In Jquery, use live to bind events. If you want to remove the event, use the die method.

For example:

$(“.myDiv”).die("click");
Copy after login

This will remove the bound click event.

3.JQuery offset(), position() method to obtain absolute and relative position coordinates

To get the absolute X, Y coordinates of an element on the page, you can use the offset() method: (body attribute setting margin: 0; padding: 0;)

var X = $('#DivID').offset().top; 
var Y = $('#DivID').offset().left; 
Copy after login

For example:

$(".produc a span").click(function(){
 $('body, html').animate({scrollTop:$('#buy').offset().top }, 'slow');
 });
Copy after login

Get relative (parent element) position:

var X = $('#DivID').position().top; 
var Y = $('#DivID').position().left; 
var left = $("selector").offset().left;//元素相当于窗口的左边的偏移量
var top = $("selector").offset().top;//元素相对于窗口的上边的偏移量
var pleft = $("selector").scrollLeft();//元素相对于滚动条左边的偏移量
var pTop = $("selector").scrollTop();//元素相对于滚动条顶部的偏移量
Copy after login

4.jquery to get the mouse position

 $(function () {
      //e为事件名;
      $(document).mousemove(function (e) {
        $("p").text("X:" + e.pageX + "  Y:" + e.pageY);
      });

    });
Copy after login

5.jquery判断某个元素是否含有某个class,是否存在某些属性,怎样移除某些属性。

在JQuery编码中,我们会判断元素是否存在某个属性.比如是否包含 class="new" 的样式呢.JQuery判断就非常简单了,因为有 hasClass这个方法 $("input[name=new]").hasClass("new") 即可判断.

这时就没有现成的方法了. 如果存在某个属性 $("#aid").attr("rel") 会返回 rel的值,如果不存在 rel属性则会返回"undefined"
undefined 就是 undefined类型 , if($("#aid").attr("rel")=="undefined") 这个判断可能不成立.
因为类型不相同.

建议使用 if(typeof($("#aid").attr("rel"))=="undefined") 即可。

jquery移除某个jquery对象的某个属性: $(".main").removeAttr("style");

6.jquery stop()的用法(清除动画积累的有效方法)

1、stop([stopAll], [gotoEnd])方法有两个参数(当然可以不传或直传一个),其中stopAll的意思是清除之后的所有动画。gotoEnd的意思是,执行完当前动画。

2、stopAll == true时,停止队列中的所有动画, stopAll ==false时,只停止队列中的当前动画,后续动画继续执行。

3、gotoEnd == true时,立即跳到当前动画的末尾, gotoEnd ==false时,停在当前状态。且gotoEnd只有在设置了stopAll的时候才起作用

4、在项目中,如果不进行动画队列清理,就会产生动画积累的问题。因此在写入动画时,最好先清除队列中的重复动画。

在项目中,例如做下拉二级导航效果,用到jquery的slideDown()与slideUp()方法,当鼠标快速晃动后,如果不进行动画队列清理,就会产生动画积累,出现问题。

例如:

$(".nav li.has_list").hover(function(){
  $(this).children("a").addClass("curr");
  $(".nav li.has_list").children("div").stop(false,true);
  $(this).children("div").slideDown(400).end();              
},function(){
  $(this).children("a").removeClass("curr");
  $(".nav li.has_list").children("div").stop(false,true);
  $(this).children("div").slideUp(400).end();
  }
);
Copy after login

以上内容就是本文关于jquery常用函数与方法汇总,希望大家喜欢。

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