jQuery 保存局部变量以供稍后在代码中使用

发布于 2024-11-10 12:40:29 字数 702 浏览 3 评论 0原文

无论如何,我可以在函数之外保存或访问局部变量吗?考虑下面的代码:

$( "#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)的值以供以后在函数外部使用?

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

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' );
        }
    });

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み零 2024-11-17 12:40:29

试试这个:

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

或者在函数外部声明你的变量。

var example;

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

abc();

console.log(example);

try this:

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

or declare your variable outside the function.

var example;

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

abc();

console.log(example);
九厘米的零° 2024-11-17 12:40:29
var jdc;
var jdi;    

$( "#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');

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

            $(this).droppable( 'disable' );
    }
});
var jdc;
var jdi;    

$( "#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');

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

            $(this).droppable( 'disable' );
    }
});
多孤肩上扛 2024-11-17 12:40:29

您始终可以将它们存储为元素上的数据:

$(this).data('jdi', $('img').attr('id'));

然后您可以通过再次调用“.data()”从该元素中获取它:

var theSavedJdi = $('whatever').data('jdi');

You could always just store them as data on the element:

$(this).data('jdi', $('img').attr('id'));

Then you can get it back from that element with another call to ".data()":

var theSavedJdi = $('whatever').data('jdi');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文