如何使变量取数值?

时间:2017-04-12 19:37:19

标签: javascript

如果可能, 如何使nam存储整数100?

在javascript中,

    var ClubsK = 100;
var str = "file:///C:/Users/Lopchan/Desktop/black%20jack/images/ClubsK.png";
var nam = str.substring(str.lastIndexOf("/")+1, str.lastIndexOf("."));
document.getElementById("demo").innerHTML = ClubsK; //gives 100
document.getElementById("demo").innerHTML = nam; //gives ClubsK

1 个答案:

答案 0 :(得分:1)

我想你要做的是访问一个动态变量,在这种情况下,我建议使用一个对象。

var data = {
  ClubsK: 100
};

var str = "file:///C:/Users/Lopchan/Desktop/black%20jack/images/ClubsK.png";
var nam = str.substring(str.lastIndexOf("/")+1, str.lastIndexOf("."));
document.getElementById("demo").innerHTML = data['ClubsK']; //gives 100
document.getElementById("demo").innerHTML = data[nam]; //gives 100