简体   繁体   English

JavaScript:显示和更新文本数组

[英]JavaScript: Displaying and updating text arrays

Trying to put a paragraph together on canvas with JavaScript from multiple user specified variables. 尝试使用JavaScript从多个用户指定的变量中将一个段落放到画布上。 I start off with... 我从...开始

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..
    };
}

I seem to be forgetting something, and being just a hobbyist, I'm not sure what. 我似乎忘记了一些东西,而作为一个业余爱好者,我不确定是什么。 Essentially I'm just trying to replace colorChoice with blue when blue has been selected by the user. 基本上我只是想取代colorChoiceblueblue已经被用户选择。 The selection and user.color is updating correctly when selected. 选择时,所选内容和user.color正确更新。

Have you tried 你有没有尝试过

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

EDIT 编辑

If you really want to replace substring in a string, this is how you can do it 如果您确实要替换字符串中的子字符串,则可以这样做

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM