如何从p5.js更改HTML元素中的文本?

时间:2017-08-19 00:50:39

标签: p5.js

我只是尝试了p5.js.我想从p5更改一些HTML文本(不在p5画布上)。类似的东西:

score_display = createElement("text", "Score: 0");
...
score_display.nodeValue = "Score: " + score; // doesn't work

我该怎么做? 感谢。

5 个答案:

答案 0 :(得分:1)

通过查看the reference可以最好地回答这些问题。

具体而言,您可能正在寻找html()函数(link)或value()函数(link)。

旁注:什么是<text>标签?据我所知,这不是一个有效的HTML元素。

答案 1 :(得分:1)

p5.js中有一个.html()属性,用于更改元素的innerHTML。

在参考文档中很难找到。也许他们过时了? https://p5js.org/reference/#/p5.Element/html

答案 2 :(得分:0)

我认为您只需要执行score_display.value("")即可。然后您可以将score_display更改为所需的任何值,并将括号中的空格更改为所需的值。

答案 3 :(得分:0)

转到index.html并输入以下代码:

<h1>Score : <p id = "scoreText">0</p></h1>

将其放入html中后,将此代码添加到sketch.js:

function changeText(text){
  document.getElementById("scoreText").innerHTML = text;
}

调用此函数并将分数添加到parinthisis中以更改文本

答案 4 :(得分:0)

这是旧的,但我想我会告诉你我会做的方式。

创建按钮后,设置其ID:

<tf.Tensor: shape=(1, 12), dtype=float32, numpy=
array([[ 0.06973769, -0.1798143 , -0.2920275 ,  0.84811246,  0.44899416,
        -0.10300556,  0.00831143, -0.16158538,  0.13395026,  0.4352504 ,
         0.19114715,  0.44100884]], dtype=float32)>

然后像在普通JavaScript中一样使用querySelector或getElementById进行抓取,并使用textContent设置文本。

score_display.id('scoreText');