有没有办法让以下声明持久化为所有以后添加到DOM的匹配元素?虚构的jQuery持久化方法
$("a.my-class").replaceWith("<span>Replaced</span>");
东西线沿线的...
$("a.my-class").persist().replaceWith("<span>Replaced</span>");
(坚持是一个虚构的方法,我希望传达我想完成什么。)
有没有办法让以下声明持久化为所有以后添加到DOM的匹配元素?虚构的jQuery持久化方法
$("a.my-class").replaceWith("<span>Replaced</span>");
东西线沿线的...
$("a.my-class").persist().replaceWith("<span>Replaced</span>");
(坚持是一个虚构的方法,我希望传达我想完成什么。)
没有一种方法完全像你想要什么,但如果内容正在使用jQuery AJAX方法添加,您可以使用此:
$("<div></div>").ajaxSuccess(function(){
$("a.my-class").replaceWith("<span>Replaced</span>");
});
而且此代码后就会运行每成功的AJAX请求,提供的请求是使用jQuery的$.ajax
呼叫作出(inclu ding $.post
或$.get
)。你只需要在页面上调用一次,它会在任何AJAX调用时触发。
如果用替换发生得太快了遇到麻烦:
$("<div></div>").ajaxSuccess(function(){
window.setTimeout(function(){
$("a.my-class").replaceWith("<span>Replaced</span>");
}, 250);
});
仍不能确定我完全理解。如果(在理论上)持续存在以你描述的方式存在,那么你的第二个电话会清空它! –
所以你宁愿我说这样? (“。my-class”)。persist(function(){$(this).replaceWith(“ Replacement”)}); – jerome