2016-07-17 70 views
0

嘗試使用來自多個用戶指定變量的JavaScript在畫布上放置一個段落。我開始...JavaScript:顯示和更新文本數組

function special01() { 
    //clean slate  
    ctxx.clearRect(0, 0, ctx.width, ctx.height); 

    //navigations important 
    drawNav(); 

    //lets begin 

    var newSlate = "You're feeling colorChoice today!"; 
    document.getElementById("displayed").innerHTML = 
     newSlate; 
    if (user.color == 'blue') { 
    var array = newSlate.replace("colorChoice", "blue") 
    } 
    else if (user.color == 'red') { 
    etc..etc.. 
    }; 
} 

我似乎忘記了一些東西,只是一個業餘愛好者,我不知道是什麼。基本上我只是試圖用blue代替colorChoice,當用戶選擇blue時。當選擇時,選擇和user.color正確更新。

回答

0

你試過

function special01(){ 
    ctx.clearRect(0, 0, ctx.width, ctx.height); 
    drawNav(); 
    document.getElementById('displayed').innerHTML = 'You\'re feeling ' + user.color + ' today!'; 
} 

編輯

如果你真的想在一個字符串替換子,這是你如何能做到這

var yourString = 'You\'re feel colorChoice today.'; 
var user.color = 'blue'; // For example 
var newString = yourString.split('colorChoice').join(user.color); // 'You\'re feel blue today.' 
+0

我能做到這一點。然而因爲它將繼續使用取決於選擇哪種顏色的文本,我只是使用if/else來替換句子中的關鍵字。那有意義嗎? – lira153

+0

@ lira153。我編輯了答案。這是你想要的? –