javascript - 动态更新/显示 HTML 上的内容

标签 javascript jquery html

我最近完成了 Codecademy 上的 Javascript 类(class)。其中一项任务是创建一个选择你自己的冒险计划。该程序非常基础,现在完成类(class)后,我正在寻找优化其性能的方法。我想做的是,不使用提示,而是使用输入文本框甚至“是/否”按钮。问题是我不希望故事的每个部分都有一堆单独的 HTML 文件。是否可以根据用户回答问题的方式动态更新/显示 HTML 页面上的内容。这可以用简单的 Javascript 或 jQuery 来完成吗?

var troll = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();

switch(troll) {
    case 'FIGHT':
        var strong = prompt("How courageous! Are you strong (YES or NO)?").toUpperCase();
        var smart = prompt("Are you smart?").toUpperCase();
        if(strong === 'YES' || smart === 'YES') {
            console.log("You only need one of the two! You beat the troll--nice work!");
        } else {
            console.log("You're not strong OR smart? Well, if you were smarter, you probably wouldn't have tried to fight a troll. You lose!");
        }
        break;
}

最佳答案

到目前为止,您的 javascript 进展顺利。

Is it possible to dynamically update/display the content on the HTML page

是的。您可以通过以下5 个功能获得很大的帮助:

关于javascript - 动态更新/显示 HTML 上的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35005666/

相关文章:

javascript - NODE.JS、MONGOOSE、JAVASCRIPT - 来自 promise 的值甚至不返回 null 或 undefined

javascript - 如何验证 onblur 表单中的日期

javascript - js改变下拉列表

javascript - Web应用程序中进度条的解决方案

javascript - 如何: Reset Bootstrap button state after returning a file from the controller

javascript - javascript 中的 Jquery .on() native 函数

javascript - 读取 cookie 数据并重定向

jquery - asp.net mvc jquery 删除选择列表项

javascript - 如何使用 JavaScript 删除 html 标签并保留换行符?

jquery - calendra 的上一个和下一个按钮不显示在 ui 中。如何解决这个问题?