VUE前端弹窗确认框this.$confirm content内容加颜色
在 this.$confirm 中为内容添加颜色,通常可以通过自定义样式或使用 HTML 片段来实现。具体取决于你使用的 UI 组件库(如 Element UI、Ant Design Vue、iView 等)。
以下是常见方案:
1. Element UI 的 $confirm
Element UI 支持通过 dangerouslyUseHTMLString 选项来使用 HTML,从而添加颜色。
this.$confirm(
'<span style="color: red;">删除后无法恢复,确定继续吗?</span>',
'警告',
{
dangerouslyUseHTMLString: true,
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}
).then(() => {
// 确定操作
}).catch(() => {
// 取消操作
});
多个颜色示例:
this.$confirm(
'将删除 <span style="color: #f56c6c;">3条记录</span>,其中 <span style="color: #e6a23c;">2条</span> 为永久删除',
'提示',
{
dangerouslyUseHTMLString: true,
confirmButtonText: '确定',
cancelButtonText: '取消'
}
);
2. Ant Design Vue 的 Modal.confirm
Ant Design Vue 支持使用 VNode 或自定义内容。
// 方法1:使用 VNode
const h = this.$createElement;
this.$confirm({
title: '提示',
content: h('span', [
'删除后',
h('span', { style: { color: 'red' } }, '无法恢复'),
',确定继续吗?'
]),
okText: '确定',
cancelText: '取消',
onOk() {
// 确定操作
}
});
// 方法2:使用函数渲染
this.$confirm({
title: '提示',
content: () => h('div', [
h('p', '确认删除以下项目?'),
h('p', { style: { color: '#ff4d4f' } }, '项目名称:重要文件.zip')
]),
okText: '确定',
cancelText: '取消'
});
3. iView / View UI 的 $Modal
this.$Modal.confirm({
title: '提示',
render: (h) => {
return h('span', [
'确定要删除',
h('span', { style: { color: 'red' } }, '这条数据'),
'吗?'
]);
},
onOk: () => {
// 确定操作
}
});
4. 通用方案(自定义样式类)
如果组件库支持自定义类名,可以添加全局样式:
// JS 代码
this.$confirm('这是一条重要提示消息', '提示', {
customClass: 'custom-confirm-content'
});
/* 全局样式 */
.custom-confirm-content .el-message-box__message {
color: #e6a23c;
font-weight: bold;
}
/* 或针对特定文字 */
.custom-confirm-content .highlight {
color: #f56c6c;
}
// 配合 HTML 使用
this.$confirm(
'请删除 <span class="highlight">未使用的文件</span>',
'提示',
{
dangerouslyUseHTMLString: true,
customClass: 'custom-confirm-content'
}
);
注意事项
-
安全性:使用
dangerouslyUseHTMLString时要注意防范 XSS 攻击 -
全局样式污染:使用
customClass时样式作用域要控制好 - Vue 版本:不同 UI 组件库的 API 可能略有差异,建议查阅对应文档