要修改layui layer.alert弹出框中“确定”按钮的文字,必须改用layer.open()手动构建alert结构并覆盖默认按钮配置:设置type:1、content为提示内容、btn数组指定按钮文字、yes回调中调用layer.close(index)关闭弹窗。

要修改layui layer.alert弹出框中“确定”按钮的文字,不能直接调用layer.alert()传入按钮文本,必须改用layer.open()手动构建alert结构并覆盖默认按钮配置。
用layer.open替代layer.alert并自定义按钮文字
第一步:调用layer.open(),设置type: 1(页面层),content为纯文本或HTML内容;
第二步:在btn数组中只写一个字符串,例如['知道了'],这样就只会显示一个按钮且文字为你指定的内容;
第三步:在yes回调函数中执行关闭操作,否则点击按钮不会自动关闭弹窗。
代码示例:
layer.open({
content: '操作成功!',
btn: ['【确认】'],
yes: function(index){
layer.close(index);
}
});
复用封装成自定义alert函数
方法一:简单封装,支持传入提示文本和按钮文字
复制以下代码到你的JS中即可调用:
function myAlert(msg, btnText) {
layer.open({
content: msg,
btn: [btnText || '确定'],
yes: function(index){ layer.close(index); }
});
}
调用:myAlert('密码修改成功', '好的');
方法二:兼容原layer.alert参数风格,保留title和icon支持
这个版本会自动识别是否传入了title对象,并支持icon图标:
function customAlert(content, options = {}) {
const cfg = {
content: typeof content === 'string' ? content : '',
btn: [options.btnText || '确定'],
title: options.title || false,
icon: options.icon !== undefined ? options.icon : 1,
yes: function(index) { layer.close(index); }
};
if (options.skin) cfg.skin = options.skin;
layer.open(cfg);
}
注意事项与常见错误
直接修改layer.alert源码或试图用CSS隐藏原按钮再插入新按钮,会导致点击无响应或关闭逻辑失效;
layer.alert不支持btn配置项,传入btn会被忽略——这是官方设计限制,不是bug;
【必须使用layer.open并显式声明btn数组,否则按钮文字无法变更】;
如果未定义yes回调,点击自定义按钮后弹窗不会关闭,用户只能点右上角×退出。











