WEBSITE按钮,显示数组Javascript中的随机值

时间:2016-04-27 17:02:12

标签: javascript html

http://jsfiddle.net/7118zcfc

$scope.ctx.skills = data.result.skills;
    $scope.ThingsYouWouldntLearn = [
    "Hooray",
    "Amount of mushrooms in Mario.", 
    "How to complete life.", 
    "How to create an aryan race",  
    "How paper papers", 
    "Do do do dododooooooo", 
    "A Level Physics / things you dont know = this site",
    "How to eat 200000 bananas in .23 seconds",
    "Why Jeremy Clarkson has curly hair.",
    "Starting to run out of things here",
    "Why Oli Stratford looks like James May",
  ];

 $scope.ThingsYouWouldntLearn = $scope.ThingsYouWouldntLearn[Math.floor(Math.random()*$scope.ThingsYouWouldntLearn.length)];

}

当点击jsfiddle上显示的按钮时,如何将其中一个这样的语句以相同的字体放到网页上。

1 个答案:

答案 0 :(得分:0)

这里有很多事情你需要解决。首先,看起来你正在使用Angular,但它没有包含在文件中。如果你要去Angular路线,你会看到类似的东西:

<!-- HTML -->
<button ng-click="getThingToLearn()" type="button">Learn Something</button>
<p>{{thingToLearn}}</p>

// JS
$scope.thingToLearn = '';
$scope.getThingToLearn = function () { 
    $scope.thingToLearn = $scope.ThingsYouWouldntLearn[Math.floor(Math.random()*$scope.ThingsYouWouldntLearn.length)];
}

如果您正在执行更多JavaScript样式,那么您可以执行以下操作:

<!-- HTML -->
<button onclick="getThingToLearn()" type="button">Learn Something</button>
<p id="thingToLearn"></p>

// JS
var getThingToLearn = function () { 
    var thingToLearn = document.getElementById('thingToLearn');
    thingToLearn.textContent = ThingsYouWouldntLearn[Math.floor(Math.random()...
};

希望这有助于您找到正确的方向来开始修复您的代码。