基本上渲染了一堆我不太想要的元素,所以我想把这些元素的文本渲染成不同的元素。所以我想,是的,jQuery中的.replaceWith()
会让这变得很麻烦。
$('.register .hint').replaceWith('<div class="tooltip">' + $(this).text() + '</div>');
但是,$(this)
当然是指文档,而不是我正在替换的元素。它很容易解决...
$('.register .hint').each(function () {
$(this).replaceWith('<div class="tooltip help"> <span>?</span><div class="content"><b></b><p>' + $(this).text() + '</p></div></div>');
});
我只是想知道是否有办法在第一个例子中获取我正在操作的元素?
replaceWith() 需要一个回调函数
$('.register .hint').replaceWith(function () {
return '<div class="tooltip">' + $(this).text() + '</div>';
});
演示:小提琴