lohannes
Iohannes
2017-11-04 08:46

jq中如何在方法外定义变量在方法内可以使用?

  • 函数
  • js
$(document).ready(function(){
    $('#menuInput').mouseout(function(){
        var name_one = $('#name_one').val();
        var name_two = $('#name_two').val();
        var name_three = $('#name_three').val();
        if(name_one != ""){
            $('#phoneBottomMenu_one').html(name_one);
        }else{
            $('#phoneBottomMenu_one').html("菜单一");
        }

        if(name_two != ""){
            $('#phoneBottomMenu_two').html(name_two);
        }else{
            $('#phoneBottomMenu_two').html("菜单二");
        }

        if(name_three != ""){
            $('#phoneBottomMenu_three').html(name_three);
        }else{
            $('#phoneBottomMenu_three').html("菜单三");
        }
    });
});

这几个变量如何放到外面也能用?

            var name_one = $('#name_one').val();
        var name_two = $('#name_two').val();
        var name_three = $('#name_three').val();

就像这个意思

    $(document).ready(function(){
                var name_one = $('#name_one').val();
                var name_two = $('#name_two').val();
                var name_three = $('#name_three').val();
        $('#menuInput').mouseout(function(){

                if(name_one != ""){
                    $('#phoneBottomMenu_one').html(name_one);
                }else{
                    $('#phoneBottomMenu_one').html("菜单一");
                }

                if(name_two != ""){
                    $('#phoneBottomMenu_two').html(name_two);
                }else{
                    $('#phoneBottomMenu_two').html("菜单二");
                }

                if(name_three != ""){
                    $('#phoneBottomMenu_three').html(name_three);
                }else{
                    $('#phoneBottomMenu_three').html("菜单三");
                }
            });
    });
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答