Nor*_*rse 6 javascript variables jquery
我正在尝试声明一个变量,其值是另一个当时未设置的变量.
var add = 1 + three;
var three = 3;
document.getElementById('thediv').innerHTML = add;
//results in "NaN"
Run Code Online (Sandbox Code Playgroud)
有没有办法使用Jquery/Javascript来做到这一点?
use*_*654 11
你可以把添加到一个函数,
function add() {
return 1 + (three || 0);
}
var three = 3;
document.getElementById('thediv').innerHTML = add();
Run Code Online (Sandbox Code Playgroud)
虽然,在我看来这很难遵循.我会更进一步,并进行three争论add
function add(three) {
return 1 + (three || 0);
}
document.getElementById('thediv').innerHTML = add(3);
Run Code Online (Sandbox Code Playgroud)
这是你要的吗?
var add = "1 + three";
var three = 3;
document.getElementById('thediv').innerHTML = eval(add);
Run Code Online (Sandbox Code Playgroud)