升级jQuery至3.6.1后Notify.js通知无标题文本问题求助
解决方案
1. 替换参数传递逻辑,直接构建完整HTML内容
jQuery 3.x对DOM属性解析和对象处理的规则有所调整,导致Notify.js原有的data-notify-html属性绑定逻辑失效。可以修改autoHideNotify方法,直接将标题、文本和图标拼接成完整的HTML结构传递,绕过属性绑定环节:
//auto hide notification Notification.prototype.autoHideNotify = function (style, position, title, text) { var icon = "fa fa-adjust"; if (style == "error") { icon = "fa fa-exclamation"; } else if (style == "warning") { icon = "fa fa-warning"; } else if (style == "success") { icon = "fa fa-check"; } else if (style == "custom") { icon = "md md-album"; } else if (style == "info") { icon = "fa fa-question"; } else { icon = "fa fa-adjust"; } // 直接拼接完整通知HTML结构 var notifyHtml = "<div>" + "<div class='image'><i class='" + icon + "'></i></div>" + "<div class='text-wrapper'>" + "<div class='title'>" + title + "</div>" + "<div class='text'>" + text + "</div>" + "</div>" + "</div>"; $.notify({ html: notifyHtml }, { style: 'metro', className: style, globalPosition: position, showAnimation: "show", showDuration: 0, hideDuration: 0, autoHideDelay: 5000, autoHide: true, clickToHide: true }); };
2. 简化Metro样式定义(可选)
如果不需要动态属性绑定,可以简化notify-metro.js中的样式定义,移除data-notify-html属性,避免潜在的解析冲突:
$.notify.addStyle("metro", { html: "<div>" + "<div class='image'></div>" + "<div class='text-wrapper'>" + "<div class='title'></div>" + "<div class='text'></div>" + "</div>" + "</div>", classes: { success: { "color": "#fafafa !important", "background-color": "#81c868", "border": "1px solid #33b86c" }, // 其他样式类 } });
3. 确认Notify.js版本兼容性
确保使用的Notify.js版本已适配jQuery 3.x,旧版本的Notify.js可能存在未兼容的API调用逻辑,导致参数无法正确解析。
内容的提问来源于stack exchange,提问作者rd1218
相关产品推荐
相关产品推荐

