获取元素的ID并将其设置为变量 [英] Get element's ID and set it as variable

查看:113
本文介绍了获取元素的ID并将其设置为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮:

<button class="btn btn-info continue">
    <i class="ace-icon fa fa-check bigger-110"></i>
    Continue                                        
</button>

Onclick:

$(".continue").click(function(e) {
    currForm = $(this).parents('form');
    e.preventDefault();
});

我很容易得到id: currForm.attr('id' ); ,但我可以将此id的值设置为变量。

I can get the id pretty easily: currForm.attr('id');, but can I set the value of this id as a variable.

类似于php的变量变量:

Something like php's variable variables:

$a = 'hello';
$$a = 'world';
echo $hello;

编辑:我不想更改元素的ID。我想获取此ID并将其用作javascript变量的名称。
例如,我上面提供的元素是形式,其ID ='example_id'。 currForm.attr('id')会给我 example_id 我想设置一个变量 example_id 并为其赋值。

Edit: I don't want to change element's id. I want to get this ID and use it as a name for a javascript variable. For example, the element I provided above is in a form that has ID='example_id'. currForm.attr('id') will give me example_id and I want to set a variable example_id and assign a value to it.

var example_id ='some value';

推荐答案

这里有3个选项。

您可以使用Javascript函数 eval 实现您的目标。但请注意,这是不推荐(我强调了两次,希望你理解!)。

You can use the Javascript function eval to achieve what you want. But be aware, this in not recommended (I emphasized it twice, hope you understood!).


Don不必要地使用eval!



eval()是一个危险的函数,它执行它以调用者的特权传递的代码。如果您使用可能受恶意方影响的字符串运行eval(),您最终可能会使用您的网页/扩展程序的权限在用户的计算机上运行恶意代码。更重要的是,第三方代码可以看到调用eval()的范围,这可能导致类似函数不易受影响的攻击。<​​/ p>

Don't use eval needlessly!

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.

可以这样使用:

eval('var ' + varName + ' = "something";');



窗口对象表示法(优于 eval ,仍然没有真正推荐)



此方法包括在全局窗口对象上使用JavaScript提供的对象表示法。这会污染全局窗口范围,并且可以被其他不良的JS文件覆盖。如果你想了解更多有关该主题的信息,这是一个很好的问题:在JavaScript'窗口'对象中存储变量是使用该对象的正确方法吗?

Window object notation (Better than eval, still not really recommended)

This method consist of using the object notation provided by JavaScript on the global window object. This is polluting the global window scope and can be overrided by other JS files which is bad. If you want to know more on that subject, this is a good question : Storing a variable in the JavaScript 'window' object is a proper way to use that object?.

要使用该方法,您可以这样做:

To use that method, you would do thing like that :

window[varName] = something;
alert(window[varName]);



使用对象(推荐)



最好的解决方案是创建自己的变量范围。例如,您可以在全局范围上创建变量并为其分配对象。然后,您可以使用对象表示法创建动态变量。它的工作方式与窗口相同:

Using an object (recommended)

The best solution would be to creating you own variable scope. For instance, you could create on the global scope a variable and assign an object to it. You can then use the object notation to create you dynamics variables. It work the same way as the window does :

var globalScope = {};

function awesome(){
    var localScope = {};
    globalScope[varName] = 'something';
    localScope[varName] = 'else';

    notSoAwesome();
}

function notSoAwesome(){
    alert(globalScope[varName]); // 'something';
    alert(localScope[varName]); // undefined
}

这篇关于获取元素的ID并将其设置为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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