移动图像... javascript将变量声明为字符串(Moving an image … javascript declare variables as string)

编程入门 行业动态 更新时间:2024-10-27 20:29:07
移动图像... javascript将变量声明为字符串(Moving an image … javascript declare variables as string)

单击按钮时,包含位置的变量将声明为字符串! 这是html代码: http : //insane96mcp.altervista.org/corsa.html这里是javascript代码: http : //insane96mcp.altervista.org/corsa.js

When you click a button the variable which contains the position is declared as string! This is the html code: http://insane96mcp.altervista.org/corsa.html And here's the javascript code: http://insane96mcp.altervista.org/corsa.js

最满意答案

问题出在这一行:

pxMove=(Math.random()*10).toFixed();

toFixed将您的数字转换为字符串,然后下一行中的加号运算符将连接它而不是求和。 如果要将其舍入并转换为整数,可以使用Math.round :

pxMove = Math.round(Math.random() * 10);

The problem is with this line:

pxMove=(Math.random()*10).toFixed();

toFixed will convert your number to string, and the plus operator in the next line will then concat it instead of summing. If you want to round it and transform to integer, you can use Math.round:

pxMove = Math.round(Math.random() * 10);

更多推荐

本文发布于:2023-07-29 20:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319458.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   变量   图像   声明   variables

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!