与 的兼容性问题并调用一个函数();跨不同的网络浏览器
我是 JavaScript 新手。我编写了以下函数 rollDice() 来生成 5 个随机数并显示它们。我使用带有单击事件的锚点来调用该函数。问题是,在 Chrome 中它不会显示,在 IE 中工作正常,在 Firefox 中显示 5 个值,然后出现带有锚点的原始页面!我怀疑我的脚本标签太笼统,但我真的迷失了。另外,如果有一个不首先清除屏幕的显示功能,那就太好了。
diceArray = new Array(5)
函数 rollDice() { 变量我;
for(i=0; i<5; i++) { diceArray[i]=Math.round(Math.random() * 6) % 6 + 1;
文档.write(diceArray[i]); } }
当我点击时应该显示 5 个 rand 变量
I am new to javascript. I wrote the following function rollDice() to produce 5 random numbers and display them. I use an anchor with click event to call the function. Problem is, in Chrome it won't display, works fine in IE, in firefox the 5 values display and then the original page w/anchor appears! I am suspicious that my script tag is too general but I am really lost. Also if there is a display function that doesn't clear the screen first that would be great.
diceArray = new Array(5)
function rollDice()
{
var i;
for(i=0; i<5; i++)
{
diceArray[i]=Math.round(Math.random() * 6) % 6 + 1;
document.write(diceArray[i]);
}
}
when I click should display 5 rand variables
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Javascript 控制台中是否出现任何错误?我知道 Chrome 和 Firefox 都有它们,所以您应该能够看到是否生成了任何错误。您还可以添加一些警报(“嘿”)消息来检查它是否正在 Chrome 中启动。
我确实看到你的第一行后面没有分号,但我不知道这是直接剪切和粘贴,还是你手动输入的。
Do any errors appear in the Javascript consoles? I know that Chrome and Firefox both have them, so you should be able to see if any errors are being generated. You could also add a few alert('Hey') messages to check that it's even being launched in Chrome.
I do see that you don't have a semi-colon after your first line, but I don't know if that's a direct cut and paste, or if you typed it all in manually.