Jquery Toggle two function not work in 1.10.1

佐手、 提交于 2019-11-27 22:31:46

问题


Jquery Toggle two function not work in jquery 1.10.1 but work's on Jquery 1.8.3

HTML

<p>For example, consider the HTML:</p>
                <div id="target">
                  Click here
                </div>

Jquery

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

and expmple is here


回答1:


Dude it doesn't works in jQuery 1.10.1

Still there is another way to do it...

$(function () {
    function first() {
       //Code for first time click goes here
        $(this).one("click", second);
    }
    function second() {
        //Code for second time click goes here
        $(this).one("click", first);
    }
    $("#target").one("click", first);
});



回答2:


jQuery.toggle has been deprecated since version 1.8 and removed sometime later. You can write your own implementation instead:

function handler1() {
    alert("first handler for .toggle() called.");
    $(this).one("click", handler2);
}
function handler2() {
    alert("second handler for .toggle() called.");
    $(this).one("click", handler1);
}
$("#target").one("click", handler1);

Demo




回答3:


DEMO

DEMO jQuery 2.x (edge)

use this clickToggle plugin as toggle function is removed from version 1.9

(function ($) {
    $.fn.clickToggle = function (func1, func2) {
        var funcs = [func1, func2];
        this.data('toggleclicked', 0);
        this.click(function () {
            var data = $(this).data();
            var tc = data.toggleclicked;
            $.proxy(funcs[tc], this)();
            data.toggleclicked = (tc + 1) % 2;
        });
        return this;
    };
}(jQuery));
$('#target').clickToggle(function () {
    alert('First handler for .toggle() called.');
}, function () {
    alert('Second handler for .toggle() called.');
});



回答4:


This format was removed in version 1.9, so you cannot use it - if you want this logic can be manually implemented or you can make use of the migration plugin

to include the migration plugin, after the inclusion of jQuery library add

<script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>



回答5:


It was removed in jQuery 1.9:

This is the "click an element to run the specified functions" signature of .toggle(). It should not be confused with the "change the visibility of an element" of .toggle() which is not deprecated. The former is being removed to reduce confusion and improve the potential for modularity in the library. The jQuery Migrate plugin can be used to restore the functionality.

Complete upgrade guide: http://jquery.com/upgrade-guide/1.9/




回答6:


Here is a generic method, with multiple functions to toggle:

var toggleFunctions = [
    function () { /* do something */},
    function () { /* do something else */},
    ...
    function () { /* do some other stuff*/}
];

$("#target").on("click", function(){
    // returns first function from array
    var currentFunction = toggleFunctions.shift(); 
    // executes the function
    currentFunction(); 
    // pushes current function to the back
    toggleFunctions [] = currentFunction; 
});



回答7:


toggle is not working for me in jquery so with the help of stackoverflow my alternative is

<a href="#" id="thiclik"> Click Me To Change</a>
		
		<div id="clickevent">
		</div>
and here is implementation or you can say alternative of toggle function

$('#thiclik').click(function()
{
//toggle alternative 

	function handler1() {
		//alert("first handler for .toggle() called.");
		$("#clickevent").text("Yes");
		$('#thiclik').one("click", handler2);
	}
	function handler2() {
	//	alert("second handler for .toggle() called.");
		$("#clickevent").text("No");
		$('#thiclik').one("click", handler1);
	}
	$("#thiclik").one("click", handler1);
});
working for me but with little problem when i click first time on click me it not work but on second and third time it works fine.

来源:https://stackoverflow.com/questions/18206021/jquery-toggle-two-function-not-work-in-1-10-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!