计算要使用的先前变量的名称
我需要一种方法来查找之前加载到 document.ready() 函数中的变量的名称。变量的名称与需要再次调用它的元素的 id 相关联。
假设变量是 var myScrollscroll5
,需要再次调用该变量的元素的 id 将为 scroll5
。因此我希望这能奏效。
'myScroll' + $('.dircore').attr('id').refresh();
我们不能只调用该变量,因为名称未知,只能在元素 .dircore 的 id 中找到。
奇怪的是,这仅在 Firefox 中有效,即使 firebug 说这是一个错误。但在其他浏览器中不起作用。
I need a way of finding the name of a variable loaded in a document.ready() function earlier. The name of the variable is tied up inside the id of the element that needs to call it again.
Say the variable was var myScrollscroll5
, the id of the element that needs to call the variable again will be scroll5
. Thus I hoped this would work.
'myScroll' + $('.dircore').attr('id').refresh();
We cannot just call the variable because the name is unknown and can only be found in the id of the element .dircore.
Oddly enough this worked in firefox only even though firebug said it was an error. Doesn't work in other browsers though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能使用 jQuery .data() 函数将数据绑定到元素并稍后调用吗?
像这样:
进而:
或者类似的东西。这样您也可以免受循环引用和由它们引起的内存泄漏的影响。
Can't you use the jQuery .data() function to bind the data to the element, and recall it later?
Like this:
And then:
Or something like that. This way you're also safe from circular references and memory leaks caused by them.
您在这里需要
eval
:虽然
eval
被认为是邪恶的,但有时它可能很有用,这看起来像是其中之一,因为您不只评估您自己的内部用户输入数据。You need
eval
here:Although
eval
is considered evil, it can be useful sometimes, this looks like one of those times as you don't evaluate user input just your own internal data.如果您添加了一些可能会有所帮助的代码。对我来说,这似乎是一个全局与局部范围的问题。无论您在做什么,只需首先在全局范围内定义变量,然后在函数内设置值,无论您使用什么方法实例化变量值。例如
a)。 //全局范围内
var myvariable=null;
然后在全局或局部范围内访问该变量。
b) 其他选项可能是将变量定义为对象
欢呼:)
If you had added some code that might have been helpfull.To me it seems a global vs local scope issue . Whatever you are doing , just define the variable in global scope first and set the value inside the function whatever method you are using to instantiate the variable value . e.g
a). //in global scope
var myvariable=null;
Then access the variable in global or local scope.
b) other options might be define the variable as object
cheers :)