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

查看:24
本文介绍了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天全站免登陆