Home  >  Article  >  Web Front-end  >  What functions does jquery have?

What functions does jquery have?

藏色散人
藏色散人Original
2020-11-26 11:35:183403browse

jquery functions include: 1. "delay(duration,[queueName])" function; 2. "jQuery live(type, fn)" function; 3. ".live()" function; 4. " position()" function; 5. "stop()" function and so on.

What functions does jquery have?

Recommended: "jquery tutorial"

  • This method is suitable for all brands of computers.

Summary of common jquery functions and methods

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); 
});

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

Query New method in 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 a few major differences:

.live currently only supports a subset of all events, Refer to the description above for the list.

.live does not support the "no event" 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:

jquery:

$(“.myp”).live(“click”, function(){
alert(“clicked!”);
});

If you use javascript to dynamically create an element with class myp, 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: you don’t have to repeatedly define events when elements are updated.

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

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

2. Remove the event bound by live

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

For example:

$(“.myp”).die("click");

This will remove the bound click event.

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

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

var X = $('#pID').offset().top; 
var Y = $('#pID').offset().left;

For example:

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

Get the relative (parent element) position:

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

4.jquery gets the mouse position

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

    });

5.jquery determines whether an element contains a certain class, whether certain attributes exist, and how to remove certain attributes.

In JQuery coding, we will judge whether an element has a certain attribute. For example, whether it contains the style of class="new". JQuery judgment is very simple because there is the hasClass method $( "input[name=new]").hasClass("new") can be used to determine.

There is no ready-made method at this time. If there is a certain attribute $("#aid") .attr("rel") will return the value of rel. If the rel attribute does not exist, it will return "undefined"
undefined is the undefined type, if($("#aid").attr("rel")== "undefined") This judgment may not be true.
Because the types are different.

It is recommended to use if(typeof($("#aid").attr("rel"))==" undefined") will do.

jquery removes an attribute of a jquery object: $(".main").removeAttr("style");

6.Usage of jquery stop() (Effective method to clear animation accumulation)

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("p").stop(false,true);
  $(this).children("p").slideDown(400).end();              
},function(){
  $(this).children("a").removeClass("curr");
  $(".nav li.has_list").children("p").stop(false,true);
  $(this).children("p").slideUp(400).end();
  }
);

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

The above is the detailed content of What functions does jquery have?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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