vue2在el-table中v-html 解析后中找到文本中带有img 标签
时间: 2024-03-17 14:41:38 浏览: 259
在 `el-table` 中使用 `v-html` 渲染后,可以通过在 `cell-class-name` 属性中自定义样式,然后通过 `render` 函数获取当前单元格中的 `img` 标签,并进行判断。
具体实现步骤如下:
1. 在 `el-table` 中使用 `cell-class-name` 属性,自定义单元格的样式类名。
2. 在 `render` 函数中,获取当前单元格中的 `img` 标签,并进行判断。
以下是一个示例代码:
```html
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="content" label="内容" :cell-class-name="getCellClassName">
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{
content: '<p>这是一段包含<img src="xxx" alt="图片描述">图片的文本</p>'
}
]
}
},
methods: {
getCellClassName({ row, column, rowIndex, columnIndex }) {
return 'my-cell-class'
}
},
render: function(h, { row, column, rowIndex, columnIndex }) {
if (columnIndex === 0) {
const htmlStr = row[column.property]
const div = document.createElement('div')
div.innerHTML = htmlStr
const imgList = div.querySelectorAll('img')
imgList.forEach(img => {
if (img.alt === '图片描述') {
// 图片描述为"图片描述"的处理逻辑
}
})
return h('div', htmlStr)
}
}
}
</script>
<style>
.my-cell-class {
padding: 0 !important; /* 去除单元格内边距 */
}
.my-cell-class > div {
padding: 10px; /* 设置单元格内边距 */
}
</style>
```
在上面的代码中,我们在 `getCellClassName` 方法中返回了自定义的样式类名 `my-cell-class`,并在样式中设置了 `padding: 0 !important` 去除单元格内边距,然后在 `render` 函数中获取当前单元格的 `htmlStr`,并使用 `querySelectorAll` 方法获取其中的 `img` 标签,进行判断。最后,使用 `h` 函数生成一个新的 `div` 元素,并将 `htmlStr` 作为其子元素返回。
阅读全文
相关推荐


















