fix(layui): 修复 layui.link 重复执行时不触发回调函数的问题 (#2651)

* fix(layui.js): 修复 layui.link 重复执行时回调不触发的问题

这会导致 laydate 无法渲染,行为和 2.9 保持一致

* chore(link): 修改注释错别字

* chore(link): 添加注释

---------

Co-authored-by: 贤心 <3277200+sentsim@users.noreply.github.com>
pull/2654/head
morning-star 2025-04-21 00:22:53 +08:00 committed by GitHub
parent 985803ec09
commit b3bfc92374
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 15 additions and 10 deletions

View File

@ -421,7 +421,7 @@
Class.prototype.link = function(href, callback, id) {
var that = this;
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
var hasCallback = typeof callback === 'function';
// 若第二个参数为 string 类型,则该参数为 id
if (typeof callback === 'string') {
@ -440,26 +440,31 @@
});
}
// 若传入 id ,则取路径 `//` 后面的字符拼接为 id不含.与参数
// 若传入 id ,则取路径 `//` 后面的字符拼接为 id不含.与参数
id = id || href.replace(/^(#|(http(s?)):\/\/|\/\/)|\.|\/|\?.+/g, '');
id = 'layuicss-'+ id;
link.href = href + (config.debug ? '?v='+new Date().getTime() : '');
link.rel = 'stylesheet';
link.id = id;
var link = document.getElementById(id);
// 插入节点
if (!document.getElementById(id)) {
// 初始创建节点
if (!link) {
link = document.createElement('link');
link.href = href + (config.debug ? '?v='+new Date().getTime() : '');
link.rel = 'stylesheet';
link.id = id;
head.appendChild(link);
}
// 是否执行回调
if (typeof callback !== 'function') {
// 若加载已完成,则直接执行回调函数
if (link.__lay_readyState__ === 'complete') {
hasCallback && callback(link);
return that;
}
// 初始加载
onNodeLoad(link, function() {
callback(link);
link.__lay_readyState__ = 'complete';
hasCallback && callback(link);
}, function() {
error(href + ' load error', 'error');
head.removeChild(link); // 移除节点