uniapp关键字标红
时间: 2023-07-24 10:28:35 浏览: 160
可以通过在uniapp中使用组件`<rich-text>`,并在其中设置`nodes`属性来实现对关键字的标红。具体的做法是,在`nodes`属性中使用`<span>`标签来包裹关键字,并设置`style`属性为`color: red`。例如:
```html
<rich-text :nodes="nodes"></rich-text>
```
```javascript
data() {
return {
text: "这是一段包含关键字的文本,关键字为uniapp",
nodes: []
}
},
mounted() {
const keyword = "uniapp";
const index = this.text.indexOf(keyword);
if (index !== -1) {
const start = this.text.slice(0, index);
const end = this.text.slice(index + keyword.length);
this.nodes = [
{
type: "text",
text: start
},
{
name: "span",
attrs: {
style: "color: red;"
},
children: [
{
type: "text",
text: keyword
}
]
},
{
type: "text",
text: end
}
];
} else {
this.nodes = [
{
type: "text",
text: this.text
}
];
}
}
```
阅读全文