我开始在W3schools中阅读JavaScript并测试/更改他们给出的示例中的一些内容,这样我就可以看到正在做什么但却无法识别语法.
以下是原始代码更改p标签的内容, 将链接到它.
<p id="demo">
JavaScript can change the content of an HTML element.
</p>
<script>
function myFunction()
{
x = document.getElementById("demo"); // Find the element
x.innerHTML = "Hello JavaScript!"; // Change the content
}
</script>
<button type="button" onclick="myFunction()">Click Me!</button>
Run Code Online (Sandbox Code Playgroud)
我想知道如何使用相同的类更改内容,但失败了,因为您可以看到下面的示例不起作用.下面的代码小提琴.
<p class="demo">
JavaScript can change the content of an HTML element.
</p>
<p class="demo">Yolo</p>
<script>
function myFunction()
{
x = document.getElementsByClassName("demo"); // Find the element
x.innerHTML = "Hello JavaScript!"; // Change the content
}
</script> …Run Code Online (Sandbox Code Playgroud) 我的画布上有一个圆圈.鼠标位置是相对于画布计算的.我希望当鼠标距离它<= 100px时,圆圈会移动.开始移动的最小距离为100px,为0.5px/tick.它在20px距离处达到2px/tick.
到目前为止,当距离小于或等于100时,我移动圆圈 - (我正在使用画架库)
function handleTick() {
distance = calculateDistance(circle, mX, mY);
if (distance<=100) {
circle.x += 0.3;
stage.update();
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的是
function handleTick() {
distance = calculateDistance(circle, mX, mY);
if (distance<=100) {
circleSpeed = // equation that takes distance and outputs velocity px/tick.
circle.x += circleSpeed;
stage.update();
}
}
Run Code Online (Sandbox Code Playgroud)
所以我认为这是一个数学问题并将其发布在数学交换上,但到目前为止还没有答案.我试过谷歌搜索几个主题,如:"如何得出一个关系的方程",因为我有域(100,20)和范围(0.5,2).什么功能可以与他们联系?
事情是我不擅长数学,这些数字甚至可能没有关系 - 我不确定我在这里寻找什么.
我应该写一个随机算法" circleSpeed = 2x + 5x;"并希望它做我想要的吗?或者有可能像我一样 - "我希望这些是最小值和最大值,现在我需要为它提出一个等式"?
向右方向的指针会很棒,因为到目前为止我在黑暗中拍摄.