【发布时间】:2010-11-11 15:04:10
【问题描述】:
我正在使用下面的代码将内容插入页面。我需要在文本中插入一些变量以生成一些动态值,但不确定如何执行此操作。谁能告诉我需要改变什么?
这是我如何在 JS 中存储数据的精简示例:
var fruit = {
'apples': {
'goldenDelicious' : {
color: 'yellow',
sale: 'A sale of $XXXXX (need to insert a number in here). Be sure to buy some before they are all gone!',
link: {
linkText: ('This company sells this for $XXXXX (eed to insert a price here). Click here to visit their site.'),
url: 'I NEED TO INSERT THE URL HERE'
}
}
}
}
这是我如何称呼它的一个例子:(它比这更动态,但你明白了)
var fruitType = 'apples';
var fruitVariety = 'goldenDelicious';
fruit[fruitType][fruitVariety], function () {
// do something
});
所以我希望能够在对象中插入价格、销售和 URL 等值。
【问题讨论】:
-
您应该保持数据原样,并在呈现它们时进行转换(我猜是 HTML 格式)。这正是 javascript 模板引擎所做的。
标签: javascript arrays object