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

Introduction to the replacement method of die, browser, live, sub, toggle deleted in jquery1.9

黄舟
Release: 2017-06-26 09:52:15
Original
1753 people have browsed it

jQuery 1.9 has several changes. The most important ones are browser, live, die, sub, and toggle mentioned in the title. If you encounter that the advanced version does not work during use, but the lower version does. time, then you need to understand whether it is the impact of version upgrade. I have also reprinted an article "API Guide for Upgrading and Deleting jQuery 1.9" before. The article introduces many aspects. In fact, I have never used many of them. I am also familiar with browser, live, and toggle. That’s all. ·


jQuery.browser()

The official recommended modification method is to distinguish different browsers through conditional judgment ·

<!--[if lt IE 9]><script src="http://m.cnblogs.com/109793/jquery-1.9.0.js" rel="nofollow"/><![endif]-->
<!--[if gte IE 9]>
<script src="http://m.cnblogs.com/109793/jquery-2.0.0.js" rel="nofollow"/>
<![endif]-->
Copy after login


If you must continue to use jQuery.browser(), you can add the "jquery-browser" plug-in, but I have not tested this plug-in.

For your own project feature detection needs, we strongly recommend using an external library, such as Modernizr, rather than relying on properties on jQuery.support. (Because jQuery internally supports these methods for detection, they are executed every time the page is loaded, but certain properties are removed when jQuery's internal code no longer requires them.)·

.live()

.live() method will be removed in 1.9, @ZPS has informed everyone in the email. Regarding the removal of the .live() method, the upgrade is relatively simple, just replace ".live()" with ".on()".

.die()

Relative to the removal of the “.live()” method, the “.die()” method is also removed from 1.9 and replaced by “.off( )"method. Just like before 1.9, many people only paid attention to the ".live()" method, but didn't know that there was another ".die()" method. There may be Coder who didn't know how to remove the added by .on(). Event is actually an event that is deleted and added using ".off()".

jQuery.sub()

.sub() method can create a new copy of jQuery without affecting the original jQuery object, my understanding of this method Yes: In fact, the .sub() method is to add or rewrite the jQuery method or create a new plugin, which needs to be discussed.

Judging from the upgrade guide above, the .sub() method has not been removed, but has been moved to other plugins, so it should still be usable, as long as the corresponding plugin is referenced.

Officially gives two specific situations for using .sub(): one is to provide a simple way to rewrite jQuery methods without changing the original method, and the other is to help users solve jQuery Plugin encapsulation and basic namespace.

.toggle(function, function, … )

The following jQuery plug-in can restore the toggle function of 1.8. If you need, you can directly copy the following code into your jQuery. , and then use the toggle function as usual.

//toggle plugin from caibaojian.com
$.fn.toggler = function( fn, fn2 ) {
    var args = arguments,guid = fn.guid || $.guid++,i=0,
    toggler = function( event ) {
      var lastToggle = ( $._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
      $._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
      event.preventDefault();
      return args[ lastToggle ].apply( this, arguments ) || false;
    };
    toggler.guid = guid;
    while ( i < args.length ) {
      args[ i++ ].guid = guid;
    }
    return this.click( toggler );
  };
Copy after login

In fact, the reason for removing toggle is mainly because it will cause confusion, and it can already be implemented in jquery, so this is redundant.

We can modify

//code from http://caibaojian.com/jquery1-9-tutorial.html
$(&#39;#example&#39;).click(function(){$("#exampleBox").toggle();})
Copy after login

like this to

$(&#39;#example&#39;).click(function(){
    if($("#exampleBox").is(":visible")){
        $("#exampleBox").hide();
        do stuff
    }else{
        $("#exampleBox").show();
        do stuff
    }
})
$("#example").hover(function(){$("#exampleBox").toggle(); })
Copy after login

to

$("#example").hover(function(){
$("#exampleBox").show();
},function(){
$("#exampleBox").hide();
});
Copy after login

Wouldn’t it be clearer

Or you can also use toggleClass to show and hide by adding classes.



The above is the detailed content of Introduction to the replacement method of die, browser, live, sub, toggle deleted in jquery1.9. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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