jQuery保存局部变量以供以后在代码中使用 [英] jQuery save local variable for use later on in the code

查看:108
本文介绍了jQuery保存局部变量以供以后在代码中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,我是否可以保存或访问其功能之外的局部变量?考虑下面的代码:

Is there anyway that I can save or access a local variable outside of it's function? Consider the code below:

$( "#droppable2" ).droppable({
        activeClass: "ui-state-hover",
        hoverClass: "ui-state-active",
        accept: "#draggable3",
        drop: function( event, ui ) {

            jdc = $(this).attr("id"); //I need to use this value later
            $( this )
                .addClass( "ui-state-highlight" );
                var x = ui.helper.clone();   
                x.appendTo('body');

                var jdi = $("img").attr("id");// I need to use this value later

                $(this).droppable( 'disable' );
        }
    });

反正有没有要获取两个变量(上面的jdc和jdi)的值以供以后在函数外部使用?

Is there anyway to get the values of the two variables (jdc and jdi above) for later use outside of the function?

最终目的是获取可放置容器的ID和所放置元素的内容.

The ultimate goal is to get id of droppable container and content of dropped element.

推荐答案

尝试一下:

jQuery(element).data(key,value);
// Store arbitrary data associated with the matched elements.

或在函数外声明变量.

var example;

function abc(){
   example = "12345";
}

abc();

console.log(example);

这篇关于jQuery保存局部变量以供以后在代码中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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