gpt4 book ai didi

jQuery 在执行之前等待replaceWith 完成

转载 作者:行者123 更新时间:2023-12-01 01:23:27 28 4
gpt4 key购买 nike

我用replaceWith批量替换了许多div中的html。替换后,我使用 jTruncate 来截断文本。然而它不起作用,因为在执行时,replaceWith 还没有完成。

我尝试了回调技巧 ( How do I extend jQuery's replaceWith function to accept a callback function? ),但没有成功。

有什么想法吗?

$(".hotel_info").each(function () {
var div;
div = $(this);
div.replaceWith(data);
truncInfo(div);
});

function truncInfo(div) {
div.jTruncate({
length: 100,
minTrail: 100,
moreText: '[more...]',
lessText: '[less...]',
ellipsisText: '...',
moreAni: 'fast',
lessAni: 'fast'
});
}

最佳答案

好的,通过这篇文章找到了解决方案:jQuery replaceWith find new element

看起来替换后,该对象已从 DOM 中删除,因此创建了一个新对象。所以,我必须像这样更改代码:

$(".hotel_info").each(function () {
var div;
var div2;
div = $(this);
div2 = div.replaceWithPush(data);
truncInfo(div2);
});

$.fn.replaceWithPush = function (a) {
var $a = $(a);
this.replaceWith($a);
return $a;
};

感谢大家抽出时间!

关于jQuery 在执行之前等待replaceWith 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24429210/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com