问题描述
我用replaceWith批量替换许多div中的html。替换后我使用jTruncate来截断文本。但它不起作用,因为在执行时,replaceWith没有完成。
I bulk replace html in many divs with replaceWith. After replacing I am using jTruncate to truncate the texts. However it doesn't work, because at the time of execution, replaceWith is not done.
我尝试了回调技巧()但是没有用。
I tried the callback trick (How do I extend jQuery's replaceWith function to accept a callback function?) but didn't work.
任何想法?
$(".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'
});
}
推荐答案
好的,找到解决方案谢谢发表到这篇文章:
Ok, found the solution thanks to this post: jQuery replaceWith find new element
似乎在替换后,对象从DOM中删除,因此创建了一个新对象。所以,我不得不改变这样的代码:
It seems that after replace with, the object is removed from the DOM, so a new one is created. So, I had to change the code like this:
$(".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完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!