jQuery 保存局部变量以供稍后在代码中使用
无论如何,我可以在函数之外保存或访问局部变量吗?考虑下面的代码:
$( "#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
或者在函数外部声明你的变量。
try this:
or declare your variable outside the function.
您始终可以将它们存储为元素上的数据:
然后您可以通过再次调用“.data()”从该元素中获取它:
You could always just store them as data on the element:
Then you can get it back from that element with another call to ".data()":