[英]JavaScript: Displaying and updating text arrays
嘗試使用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..
};
}
我似乎忘記了一些東西,而作為一個業余愛好者,我不確定是什么。 基本上我只是想取代colorChoice
與blue
當blue
已經被用戶選擇。 選擇時,所選內容和user.color
正確更新。
你有沒有嘗試過
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.'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.