html:
<audio></audio>
js:
this.audio = new Audio();
this.audio.src = 'https://dict.youdao.com/speech?audio=' + this.list[i].name;
this.audio.play();
demo示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
list-style: none;
margin: 0;
padding: 0;
}
.demo {
border: 1px solid #ccc;
width: 300px;
height: 450px;
margin: 0 auto;
}
.demo>p {
text-align: center;
font-weight: bold;
padding: 5px 0;
}
.demo ul {
width: 100%;
height: 100%;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
padding: 20px;
box-sizing: border-box;
}
.demo ul li {
width: 100px;
height: 60px;
border-radius: 8px;
background: fuchsia;
box-shadow: 0 0 5px #ccc;
cursor: pointer;
}
.demo ul li p {
text-align: center;
}
</style>
</head>
<body>
<div id="app">
<div class="demo">
<p>选择你熟悉的单词</p>
<ul>
<li :style="{background:(item.ischose?'fuchsia':'white')}" @click="chose(index)"
v-for="(item,index) in list">
<p>{{item.name}}</p>
<p v-if="item.ischose">{{item.text}}</p>
</li>
</ul>
<audio></audio>
</div>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
list: [{
name: 'Rose',
text: '玫瑰',
ischose: false
},
{
name: 'Klein',
text: '玫瑰',
ischose: false
},
{
name: 'Carroll',
text: '玫瑰',
ischose: false
}, {
name: 'Frazier',
text: '玫瑰',
ischose: false
}, {
name: 'Hudson',
text: '玫瑰',
ischose: false
}, {
name: 'Gilbert',
text: '玫瑰',
ischose: false
}, {
name: 'Elliott',
text: '玫瑰',
ischose: false
}, {
name: 'McLaughlin',
text: '玫瑰',
ischose: false
}, {
name: 'Powers',
text: '玫瑰',
ischose: false
}, {
name: 'Reyes',
text: '玫瑰',
ischose: false
}
],
},
methods: {
chose(i) {
console.log(i)
this.list[i].ischose = !this.list[i].ischose
this.audio = new Audio();
this.audio.src = 'https://dict.youdao.com/speech?audio=' + this.list[i].name;
this.audio.play();
}
},
})
</script>
</html>