jQuery切换()函数不起作用 [英] jQuery toggle() function not working

查看:120
本文介绍了jQuery切换()函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题以前可能会被问到。但我找不到这个错误。这里是来自新波士顿的视频教程。

  $(document).ready(function(){
$('#btn')。toggle(function (){
$('#msg')。html('yes');
},function(){
$('#msg')。html('No') ;
});
});

这个视频显示当点击第一个div文本时和下一个它显示的时间不是
但是当我尝试,当页面加载时,按钮淡出和div显示。为什么?可能有其他方法来实现此功能。但为什么这不起作用。他将切换语法显示为

  $('#btn')。toggle(function(){code},function {码}); 


解决方案

http://api.jquery.com/category/deprecated/


注意:此方法签名在jQuery 1.8中被弃用,并且在jQuery 1.9中删除了
。 jQuery还提供了一个名为
.toggle()的动画方法,用于切换元素的可见性。
动画或事件方法是否被触发取决于
传递的参数集。




在OP评论后更新



DEMO

a>



如果您想让此代码在任何版本的jQuery中都可以使用

  $(document).ready(function(){
var arr = ['yes','no'],
i = 0;
$('#btn') .click(function(){
$('#msg')。html(arr [i ++%2]);
});
});

如果您想使用 toggle-event DEMO

在将来使用类似功能



使用 .one()

  $(document).ready(function(){
function handle1(){
$('#msg')。html('yes');
$('#btn')。one('click',handle2);
}

函数handle2(){
$('#msg')。html('no');
$('#btn')。one('click',handle1);
}
$('#btn')。one('click',handle1);
});


Toggle在 1.9版中删除,所以你不能使用它 - 如果你希望这个逻辑可以手动实现,或者您可以使用迁移插件



包含迁移插件,包含jQuery库后添加

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


This question may be asked before. But I cant find the error. Here this is from the video tutorial of new boston.

$(document).ready(function(){
$('#btn').toggle(function(){
    $('#msg').html('yes');
},function(){
    $('#msg').html('No');
});
});

The video showing that when click first the div text is yes and next time it show no But when I try, when the page loads, the button fadeout and div show no. Why? There may be other methods to achieve this functionality. But why this dont work. He showing the syntax of toggle as

$('#btn').toggle(function(){code},function{code});

解决方案

http://api.jquery.com/category/deprecated/

Note: This method signature was deprecated in jQuery 1.8 and removed in jQuery 1.9. jQuery also provides an animation method named .toggle() that toggles the visibility of elements. Whether the animation or the event method is fired depends on the set of arguments passed.

http://api.jquery.com/toggle-event/

Updated after OP's comment

DEMO

if you want to make this code work in any version of jQuery

$(document).ready(function () {
    var arr = ['yes', 'no'],
        i = 0;
    $('#btn').click(function () {
        $('#msg').html(arr[i++ % 2]);
    });
});

DEMO

if you want to make use of toggle-event like functionality in future

use .one()

$(document).ready(function () {
    function handle1() {
        $('#msg').html('yes');
        $('#btn').one('click', handle2);
    }

    function handle2() {
        $('#msg').html('no');
        $('#btn').one('click', handle1);
    }
    $('#btn').one('click', handle1);
});

or

Toggle 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>

这篇关于jQuery切换()函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆