javascript - jQuery - update function with deprecated toggle() -
this question has answer here:
i update website jquery 1.10 use function deprecated toggle()
.
i remember, having hard time make function works in first time, exists function replace toggle()
without changing code.
i not jquery expert , appreciated.
css:
fieldset.collapsed * { display: none; } fieldset.collapsed h2, fieldset.collapsed { display: block !important; } fieldset.collapsed h2 { background-image: url(../img/nav-bg.gif); background-position: bottom left; color: #999; } fieldset.collapsed .collapse-toggle { background: transparent; display: inline !important; }
jquery:
var spath=window.location.pathname; (function ($) { $(document).ready(function () { function show () { // show $(this).text(gettext("hide")) .closest("fieldset") .removeclass("collapsed") .trigger("show.fieldset", [$(this).attr("id")]); window.localstorage.setitem($(this).attr("id"), true); } function hide () { // hide $(this).text(gettext("show")) .closest("fieldset") .addclass("collapsed") .trigger("hide.fieldset", [$(this).attr("id")]); window.localstorage.removeitem($(this).attr("id")) return false; } // add anchor tag show/hide link $("fieldset.collapse").each(function (i, elem) { // don't hide if fields in fieldset have errors key = 'fieldsetcollapser' + + spath; if (typeof (window.localstorage) != 'undefined') { var item = $(elem) .addclass("collapsed") .find("h2") .first() .append(' (<a id=' + key + ' " class="collapse-toggle" href="#">' + gettext("show") + '</a>)' ).find('a'); if (window.localstorage.getitem(key)) { //alert('show') show.apply(item); $(item).toggle(hide, show); }else { if ($("ul.errorlist").length >0) { //alert('yo show') show.apply(item); $(item).toggle(hide, show); }else{ $(item).toggle(show, hide); //alert("hide") } } } else { throw "window.localstorage, not defined"; } }); });
edited:see how works here (working jquery 1.6)
the reason .toggle() function deprecated because of confusion this!
what's going on code calling (on alternating basis) own internal "hide" , "show" functions. .toggle(hide,show)
call takes care of you.
however, inside of hide() , show() functions, you're not hiding or showing anything. you're doing adding or removing class, may or may not hide or show something.
the solution
the solution alternatively call these 2 functions change 'click' event each time 1 of functions called.
at bottom of show() code, need add:
$(this).one("click", hide);
at bottom of hide() code, need add:
$(this).one("click", show);
finally, need replace calls .toggle() these calls:
$(item).one("click", hide); // replaces $(item).toggle(hide,show); $(item).one("click", show); // replaces $(item).toggle(show,hide);
why not .is(":visible")?
quite simply, class adding/removing "collapsed" class. class does not hide $(item). because of $(this).is(":visible") always true!
clearly, won't work.
here demonstration illustrates point: jsfiddle
fully in code
for read code instead of words:
function firstevent() { // e.g. "hide" //first event code $(item).one("click", secondevent); } function secondevent() { // e.g. "show" //second event code $(item).one("click", firstevent); } $(item).one("click", firstevent); // replaces $(item).toggle(firstevent, secondevent);
Comments
Post a Comment