2013-01-13 97 views
21

this jsfiddlefn.toggle(handler(eventObject),handler(eventObject)...)哪裏有?

它採用 - 不要與toggle被confued - jQuery的版本設置爲EDGE

它突然停止工作並刪除我想作爲觸發細胞,因爲它明顯地恢復到toggle

我找不到任何貶低的標籤或如

http://api.jquery.com/category/deprecated/給出了404

如果我添加了Migrate模塊我jsFiddle then works,我看到控制檯(由https://github.com/jquery/jquery-migrate/blob/master/warnings.md闡述張貼由弗雷德裏克·哈米迪)警告

我看到Deprecate fn toggleissue 24Ticket #11786但我不希望看到它。

我在想什麼,在哪裏可以找到替代品和文檔?

注:據我瞭解棄用的原因,我只是找不到的折舊

$('#tbl .xx').toggle(
    function() { 
    $(this).siblings().each(function(){ 
     var t = $(this).text(); 
     $(this).html($('<input />',{'value' : t})); 
    }); 
    }, 
    function() { 
    $(this).siblings().each(function(){ 
     var inp = $(this).find('input'); 
     if (inp.length){ 
     $(this).text(inp.val()); 
     } 
    }); 
    }  
); 

守則MIGRATE官方文檔:

jQuery.fn.toggle = function(fn, fn2) { 
    // Don't mess with animation or css toggles 
    if (!jQuery.isFunction(fn) || !jQuery.isFunction(fn2)) { 
    return oldToggle.apply(this, arguments); 
    } 
    migrateWarn("jQuery.fn.toggle(handler, handler...) is deprecated"); 
    // Save reference to arguments for access in closure 
    var args = arguments, 
    guid = fn.guid || jQuery.guid++, 
    i = 0, 
    toggler = function(event) { 
    // Figure out which function to execute 
    var lastToggle = (jQuery._data(this, "lastToggle" + fn.guid) || 0) % i; 
    jQuery._data(this, "lastToggle" + fn.guid, lastToggle + 1); 
    // Make sure that clicks stop 
    event.preventDefault(); 
    // and execute the function 
    return args[ lastToggle ].apply(this, arguments) || false; 
    }; 
    // link all the functions, so any of them can unbind this click handler 
    toggler.guid = guid; 
    while (i < args.length) { 
    args[ i++ ].guid = guid; 
    } 
    return this.click(toggler); 
}; 

UPDATE我曾問如果他們可以保持代碼爲fn.toggler所以它是一個重命名而不是刪除

+1

根據[bugs.jquery.com]中的[ticket#11786](http://bugs.jquery.com/ticket/11786)(http:// bugs .jquery.com),似乎[toggle event](http://api.jquery.com/toggle-event/)已被棄用/棄用;但你說得對,我無法在[toggle event](http://api.jquery.com/toggle-event/)的頁面中看到。我建議你在票上添加評論。 – Pang

+0

完成,謝謝... – mplungjan

+0

我記得有一段時間聽到這個消息,並且感到困惑,它似乎沒有在前端文檔中標記爲已棄用。 –

回答

8

您可以在the list of warnings emitted by the jQuery Migrate plugin找到toggle()'s deprecation官方文檔:

JQMIGRATE:jQuery.fn.toggle(處理器,處理器...)已被棄用

原因:有兩種完全不同的含義爲.toggle() 方法。使用.toggle()來顯示或隱藏元素不受影響。 使用.toggle()作爲專門的點擊處理程序已在 1.8中棄用,並在1.9中刪除。

解決方案:重寫取決於$().toggle()的代碼,使用了jQuery遷移插件的 精縮生產版本,以提供 的功能性,或提取從 插件的源的$().toggle()方法和在應用程序中使用它。

+0

感謝您指點我。但這並不是我所期望的。 API中是否存在官方棄用或我錯過了什麼?正如你所看到的,我已經應用了MIGRATE,但沒有找到詳細闡述的.md文件 – mplungjan

+1

@mplungjan,你沒有錯過任何東西,'toggle()'的文檔頁面還沒有用這個信息更新。儘管(現在他們說'then()'和'pipe()'是一樣的東西),也許'toggle()'將成爲下一批的一部分,但API文檔剛剛被修改/更新。 –

2

jQuery 1.9還沒有最終確定。根據升級指南:

「現在,本指南是標準jQuery API文檔的附錄,這些頁面可能不會描述版本1.9的行爲。請耐心等待我們更新api.jquery.com上單個頁面的文檔以反映1.9中的更改。「

在這種情況下,我們還沒有記錄它。 !請在這裏提交文檔票:http://github.com/jquery/api.jquery.com

1

我使用的代碼:

$('#example').click(function() 
    { 
    isClicked=$(this).data('clicked'); 
    if (isClicked) {isClicked=false;} else {isClicked=true;} 
    $(this).data('clicked',isClicked); 

    if(isClicked) 
     { 
     ...do stuff... 
     } 
    else 
     { 
     ...do stuff... 
     } 
    }); 

優化的代碼(由mplungjan建議)是:

$('#example').click(function() 
    { 
    $(this).data('clicked',!$(this).data('clicked')); 

    if ($(this).data('clicked')) 
     { 
     ...do stuff... 
     } 
    else 
     { 
     ...do stuff... 
     } 
    }); 
+0

這不是很光滑。你可以用!isClicked來切換isCicked,但是使用你代碼的人需要知道你在測試之前切換,所以if必須與預期相反。 '$(this).data('clicked',!$(this).data('clicked'));' – mplungjan

+0

美麗!我通常使用不需要「壓縮」的可讀代碼。 謝謝! – Cuarcuiu

+0

不客氣。請注意:您的代碼只能處理兩次點擊。 fn.toggle的代碼處理了任何點擊。因此,每次點擊5或10次點擊或其他可能會有不同的效果。 – mplungjan

13

jquery .toggle()的棄用現在記錄在api.jquery.com。 我在forum.jquery上發現了一個很好的替代品,我現在正在使用它。它的偉大工程:)

$.fn.toggleClick = function() { 
    var functions = arguments, 
     iteration = 0; 
    return this.click(function() { 
    functions[iteration].call(); 
    iteration = (iteration + 1) % functions.length; 
    }); 
} 

用法:

$("#target").toggleClick(function() { 
    alert("First handler for .toggle() called."); 
}, function() { 
    alert("Second handler for .toggle() called."); 
}); 

編輯2014年8月6日:我重新考慮原來的代碼,發現。適用不使用正確的事情。將其更改爲.call,但沒有傳遞參數。

+0

我最終在功能[iteration] .call(this)中添加了「this」;因爲我使用的一些舊代碼是在切換處理程序中引用「this」 –