如何在jquery中替换(’something’)后获取对象的实际内容

我有这个代码

$(document).ready(function(){ $('.selector').click(function(){ obj = $(this); obj.replaceWith('
whats up man ??!
'); alert(obj.html()); }); });

我想获得’obj’的新内容,这个内容一直是​​’replaceWith’

但,

我得到旧内容而不是……

我怎样才能得到’obj’的实际内容?

我的意图是访问新的’obj’

 $('.selector').click(function(){ var obj = $(this), repl = $('
whats up man ??!
'); obj.replaceWith(repl); alert(obj.find('span').attr('class')); //this will print 'undefined' });

我想打印’span’的类名,即’medium’

您更新的方法是正确的,只需要像这样的调整:

 $('.selector').click(function(){ var obj = $(this), repl = $('
whats up man ??!
'); obj.replaceWith(repl); alert(repl.find('span').attr('class')); });

你可以在这里测试一下 。 重要的变化是repl.find()查看新元素而不是旧元素。

你为什么要那样做? 你知道你用它替换了什么….

 $(document).ready(function(){ $('.selector').click(function(){ var obj = $(this); var replacement = $('
whats up man ??!
'); obj.replaceWith(replacement); alert(replacement.html()); }); });