我使用下面的代碼將內容插入頁面。我需要在文本中插入一些變量以生成一些動態值,並且不確定如何執行此操作。有人能告訴我我需要改變什麼嗎?如何在將內容存儲在對象中時將變量插入文本?
這裏是我如何保存在我的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模板引擎所要做的。 – Mic 2010-11-11 15:58:06