效果如下
HTML
<el-select v-model="searchForm['status']" clearable @change="changeSelection">
<el-option v-for="item in emoList" :key="item.id" :value="item.text">
<img :src="item.src" style="width: 16px;height: 16px;vertical-align: text-bottom;">
</el-option>
</el-select>
JS
data:
emoList: [
{
id: 1,
src: 'src/assets/img/xmgl/red__face.png',
text: '1'
},
{
id: 2,
src: 'src/assets/img/xmgl/yellow_face.png',
text: '2'
},
{
id: 3,
src: 'src/assets/img/xmgl/green_face.png',
text: '3'
}
]
methods:
changeSelection(val) {
for (let index in this.emoList) {
let obj = this.emoList[index]
if (val !== "") {
if (obj.id == val) {
this.$refs['select'].$el.children[0].children[0].setAttribute(
'style',
`background: url(${obj.src}) no-repeat;
//将文字移走
text-indent:-9999px;
background-position: 25% center;`
)
}
} else {
//这里是为了实现清空功能
this.$refs['select'].$el.children[0].children[0].removeAttribute(
'style',
`background: url(${obj.src}) no-repeat;
text-indent:-9999px;
background-position: 25% center;`
)
}
}
},