作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码。我想用随机选择更新 results-box
。我将如何在 @click
事件上更新模板。
<template>
<div class="container">
<button @click="getDecision()" class="ui primary button">Discover</button>
<p id="results-box"></p>
</div>
</template>
<script>
export default {
name: "SlapButton",
data() {
return {
}
},
methods: {
getDecision: function () {
var decisions = [
"choice 1",
"choice 2",
"choice 3",
"choice 4",
"choice 5",
"choice 6"
]
var randNum = Math.floor((Math.random() * decisions.length) + 0);
var randChoice = decisions[randNum];
}
}
}
</script>
当用户点击按钮时,调用 getDecision()
并从 decisions
数组中随机选择一个选项。如何在 p
标记 #results-box
中显示此随机选择 randChoice
。
最佳答案
尝试下面的代码,如果这不起作用请详细说明:
<template>
<div class="container">
<button @click="getDecision" class="ui primary button">Discover</button>
<p id="results-box">
{{choice}}
</p>
</div>
</template>
<script>
export default {
name: "SlapButton",
data() {
return {
choice: '',
decisions = [
"choice 1",
"choice 2",
"choice 3",
"choice 4",
"choice 5",
"choice 6"
]
}
},
methods: {
getDecision() {
var randNum = Math.floor((Math.random() * this.decisions.length) + 0);
this.choice = this.decisions[randNum];
}
}
}
</script>
关于javascript - 如何在@click 上更新模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47657540/
我是一名优秀的程序员,十分优秀!