从ID中计算变量的变量 [英] Calculating Variables from Variables from ids

查看:180
本文介绍了从ID中计算变量的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

放置在document.ready()中的以下代码查找每个.scrollable div并创建一个变量,为每个面板提供一个id并激活滚动脚本。

  var orderit = 0; 
var scroll = {};
$('。scrollable')。each(function(){
this.id ='scrollp'+(++ orderit);
scrolls ['myScroll'+ this.id] = new iScroll(this.id,{scrollbarClass:'myScrollbar'});
});

问题在于稍后我需要通过调用来刷新这些变量, .refresh()嵌入在脚本中的方法。为此,我们需要变量名称。一种获取变量的方法是计算它的设置。

  $('。dircore')。click(function (){
'myScroll'+ $(this).attr('id')。refresh();
}

这只适用于firefox,即使firebug说它是一个错误,但它在其他浏览器中不起作用,显然不是正确的做法。



我希望有足够的信息在这里工作,但基本上我们需要使用我们想要刷新的元素的id来确定它是可变的,因此调用。 ()方法。

)函数将数据绑定到元素,然后再调用它?这样,您也可以安全地避免循环引用和内存泄漏。



像这样:
$ b


<$> $(document).ready(function(){$('#scroll5')。data(数据',价值);});

然后:


<$>'('。dircore')。each(function(){$(this).data('data')。refresh(); });

或者类似的东西。

The following code placed in document.ready() finds every .scrollable div and creates a variable giving each panel an id and activating a scrolling script.

var orderit = 0;
var scrolls = {};
$('.scrollable').each(function(){
    this.id = 'scrollp' + (++orderit);
    scrolls[ 'myScroll' + this.id ] = new iScroll( this.id, { scrollbarClass: 'myScrollbar' });
});

The problem is that later on I need to refresh these variables by calling the .refresh() method embedded in the script. To do this we need the variable name. One way to get the variable would be to calculate its setup like this.

$('.dircore').click(function(){
    'myScroll' + $(this).attr('id').refresh();
}

This works in firefox only even though firebug says it is an error, but it does not work in other browsers and is clearly not the correct way of doing this.

I hope there is enough information to work off here but essentially we need to use the id of the element we want to refresh to work out it's variable and consequently call the .refresh() method against its variable.

解决方案

Can't you use the jQuery .data() function to bind the data to the element, and recall it later? This way you're also safe from circular references and memory leaks caused by them.

Like this:

$(document).ready(function() { $('#scroll5').data('data', value ); });

And then:

$('.dircore').each(function() { $(this).data('data').refresh(); });

Or something like that.

这篇关于从ID中计算变量的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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