我需要将字符串值映射到HTML id
// values coming from JSON:
selectedMapLocation = 'status=Completed Response=76'
// I have replaced all instances of '=' with ' ' in the generated string:
var spceInBetween = selectedMapLocation.replace(/=/g, ' ');
因此输出将是:
// spceInBetween = "status Completed Response 76"
我需要将这些字符串值映射到HTML
id
。内容将是动态的,所以我不能在这里使用
slice()
方法预期输出:
<div id="content1">status</div>
<div id="content2">Completed</div>
<div id="content3">Response</div>
<div id="content4">76</div>
到目前为止我尝试过的是:
document.getElementById("content1").innerHTML = spceInBetween[0];
// returning value 's'
任何帮助将不胜感激。
提前致谢。
最佳答案
一站式解决您的问题:
var spceInBetween = selectedMapLocation.split(/=|\s/);
关于javascript - 如何将字符串值映射到HTML ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36761893/