gpt4 book ai didi

javascript - 控制台.log();那个放在

里?

转载 作者:行者123 更新时间:2023-12-02 17:22:45 29 4
gpt4 key购买 nike

我有一个小程序,其中有冰淇淋口味列表,计算机使用“console.log();”打印一个句子和味道。我已经开始了这个项目,它看起来像这样:

var randomFlavour = Math.random() * 10;

if (randomFlavour < 1) {
var randomFlavour = "chocolate";
} else if (randomFlavour < 2) {
var randomFlavour = "vanilla";
} else if (randomFlavour < 3) {
var randomFlavour = "pistachio";
} else if (randomFlavour < 4) {
var randomFlavour = "strawberry";
} else if (randomFlavour < 5) {
var randomFlavour = "cotton candy";
} else if (randomFlavour < 6) {
var randomFlavour = "cookie dough";
} else if (randomFlavour < 7) {
var randomFlavour = "bubblegum";
} else if (randomFlavour < 8) {
var randomFlavour = "peanut butter";
} else if (randomFlavour < 9) {
var randomFlavour = "mint";
} else {
var randomFlavour = "gingerbread man";
}

console.log("Hello. I would like to have" + " " + randomFlavour + " " + "ice cream please.");

问题是;这只会在控制台中打印,我希望打印的文本转换为 <p>段落,可能吗?

最佳答案

首先,您需要访问您的<p>元素。

您需要为您的元素分配一个 id:

<p id="myPar">
</p>

然后你可以像这样进入js:

var par = document.getElementById('myPar');

然后要设置其文本,您可以使用 innerHTML

par.innerHTML  = randomFlavour;

关于javascript - 控制台.log();那个放在 <p> 里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23771703/

29 4 0