将JS函数链接到Html按钮

时间:2018-06-18 19:08:46

标签: javascript html arrays function

我正在尝试用30个随机数填充数组,然后我尝试单击按钮以在浏览器中显示数组。问题是它没有显示任何东西。有人可以帮我解决这个问题吗?

这是代码:

function myFunction() {

  var myArray = new Array(30);

  for (var i = 0; i < myArray.length; i++) {

    myArray[i] = Math.floor(Math.random() * 45) + 1;
    document.getElementById("demo").innerHTML = myArray.toString(); + "," + "<br />";

  }
<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

1 个答案:

答案 0 :(得分:0)

你缺少一个结束的大括号 { "name": "MyIonicProject", "version": "0.0.1", "author": "Ionic Framework", "homepage": "http://ionicframework.com/", "private": true, "scripts": { "clean": "ionic-app-scripts clean", "build": "ionic-app-scripts build", "lint": "ionic-app-scripts lint", "ionic:build": "ionic-app-scripts build", "ionic:serve": "ionic-app-scripts serve" }, "dependencies": { "@angular/common": "^4.4.7", "@angular/compiler": "^4.4.7", "@angular/compiler-cli": "^4.4.7", "@angular/core": "^4.4.7", "@angular/forms": "^4.4.7", "@angular/http": "^4.4.7", "@angular/platform-browser": "^4.4.7", "@angular/platform-browser-dynamic": "^4.4.7", "@ionic-native/core": "^3.14.0", "@ionic-native/splash-screen": "^3.14.0", "@ionic-native/status-bar": "^3.14.0", "@ionic/storage": "^2.0.1", "cordova-android": "^6.2.3", "cordova-plugin-compat": "^1.0.0", "cordova-plugin-device": "^1.1.4", "cordova-plugin-geolocation": "^2.4.3", "cordova-plugin-splashscreen": "^4.0.3", "cordova-plugin-statusbar": "^2.2.2", "cordova-plugin-whitelist": "^1.3.1", "cordova-sqlite-storage": "^2.0.4", "ionic-angular": "^3.9.2", "ionic-plugin-keyboard": "^2.2.1", "ionicons": "^3.0.0", "rxjs": "^5.5.11", "sw-toolbox": "^3.6.0", "zone.js": "^0.8.26" }, "devDependencies": { "@ionic/app-scripts": "2.1.4", "typescript": "2.3.4" }, "description": "An Ionic project", "cordova": { "plugins": { "cordova-sqlite-storage": {}, "cordova-plugin-device": {}, "cordova-plugin-splashscreen": {}, "cordova-plugin-statusbar": {}, "cordova-plugin-whitelist": {}, "ionic-plugin-keyboard": {}, "cordova-plugin-geolocation": {} }, "platforms": [ "android" ] } } ,所以你的函数体没有“关闭”。

永远记得使用浏览器的开发控制台(按F12并转到“控制台”)来调试JavaScript,它使调试变得更加容易。

}
function myFunction() {

  var myArray = new Array(30);

  for (var i = 0; i < myArray.length; i++) {

    myArray[i] = Math.floor(Math.random() * 45) + 1;
    document.getElementById("demo").innerHTML = myArray.toString(); + "," + "<br />";

  }
}