方式一:
domain1、domain2是域名,后端提供
var wordObj = {
'domain1': {
'id1': 'xxxxxxx',
'id2': 'xxxxxxxx',
},
'domain2': {
'id1': 'xxxxxxx',
'id2': 'xxxxxxxx',
}
}
获取域名:window.location.host; window是全局对象,可以省略
中括号是动态读取的,如果是点的话是提前知道key值了
$('#a1').html(wordObj[location.host]['id']) //这里取出来了id1的值
$('#a1').html(wordObj[location.host].id) //这种方式也可以
$('#a2').html(wordObj[location.host]['id2']);
方式二:
var wordObj = {
'domain1': {
'id1': '文字1',
'id2': '文字2',
},
'domain2': {
'id1': '文字3',
'id2': '文字4',
}
}
var host = location.host;
var currentObj = wordObj[host] //获取到当前域名下的内容
Object.keys(currentObj).map(function (key) {
$(key).html(currentObj[key])
})
注意:
// Object.keys(currentObj) 可以得到当前域名下的id1 id2 id3
$(key)最好使用#拼接一下,获取DOM,它与上面的id1 id2 相对应,循环设置每个id应该对应的文本内容,避免像第一种方法对每个div设置内容。