简体   繁体   English

如何设置字符串中特定字母的样式?

[英]How to style specific letters in string?

Let's assume that I receive a response from the API that contains a summary of the last matches of a given team.假设我收到来自API的回复,其中包含指定球队最近比赛的摘要。

Example response: LLWWWWLWDWDWLW响应示例: LLWWWWLWDWDWLW

I want it to be in my div in specific style.我希望它以特定的样式出现在我的 div 中。

L should be stylized, its background should be red (square shape) and W should have a green background . L 应该是程式化的,它的背景应该是红色(方形) ,W 应该有绿色背景 The entire statement should be minimally separated from each other.整个语句应尽量彼此分开。

User has the option to change the team to check its recent matches.用户可以选择更改球队以查看最近的比赛。

I'm using Vue.js我正在使用Vue.js

One quick solution with v-for : v-for一种快速解决方案:

 const mapColors = new Map([['W', 'green'], ['L', 'red'], ['D', 'yellow']]) const app = Vue.createApp({ data() { return { matches: [{team: 'team 1', games: 'LLWWWWLWDWDWLW'}, {team: 'team 2', games: 'WWWWLWDLLWWWDW'}] }; }, methods: { setColor(c) { return mapColors.get(c) } } }) app.mount('#demo')
 .green { background: limegreen; }.red { background: lightcoral; }.yellow { background: gold; }.games { width: 1.2em; display: inline-block; text-align: center; margin: .1em; }.match { margin-right: .5em; font-weight: 600; }
 <script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script> <div id="demo"> <div v-for="(match, idx) in matches":key="idx"> <span class="match">{{ match.team }}</span> <span v-for="(game, i) in match.games":key="i"> <span class="games":class="setColor(game)"> {{ game }} </span> </span> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM