admin管理员组文章数量:1417434
I'm trying to declare a variable whose value is another variable that isn't set at that time.
var add = 1 + three;
var three = 3;
document.getElementById('thediv').innerHTML = add;
//results in "NaN"
/
Is there a way to do this using Jquery/Javascript?
I'm trying to declare a variable whose value is another variable that isn't set at that time.
var add = 1 + three;
var three = 3;
document.getElementById('thediv').innerHTML = add;
//results in "NaN"
http://jsfiddle/seSMx/1/
Is there a way to do this using Jquery/Javascript?
Share Improve this question asked May 17, 2012 at 19:48 NorseNorse 5,75716 gold badges53 silver badges86 bronze badges 5- 4 Why would you want to do that? – Alexis Pigeon Commented May 17, 2012 at 19:49
-
2
Why not
var three = 3; var add = 1 + three;
? >> jsfiddle/skram/seSMx/2 << – Selvakumar Arumugam Commented May 17, 2012 at 19:50 - If I told you, would you have an answer? – Norse Commented May 17, 2012 at 19:50
- 7 It may suggest an alternative solution if you explained what you're doing (and why). – David Thomas Commented May 17, 2012 at 19:51
- @Dasarp You might want to put that as an answer, instead of a ment; you don't get rep for ments. – Daedalus Commented May 17, 2012 at 19:54
2 Answers
Reset to default 11You could turn add into a function,
http://jsfiddle/seSMx/3/
function add() {
return 1 + (three || 0);
}
var three = 3;
document.getElementById('thediv').innerHTML = add();
Although, this would be very hard to follow in my opinion. I would take it a step farther and make three
an argument to add
function add(three) {
return 1 + (three || 0);
}
document.getElementById('thediv').innerHTML = add(3);
is this what are you going for?
var add = "1 + three";
var three = 3;
document.getElementById('thediv').innerHTML = eval(add);
本文标签: jqueryJavascript quotpre setquot a variableStack Overflow
版权声明:本文标题:jquery - Javascript "pre set" a variable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745266688a2650644.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论