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

mde*_*isa 11 jquery this replacewith

我有这个代码

$(document).ready(function(){
    $('.selector').click(function(){
        obj = $(this);
        obj.replaceWith('<div class="size">whats up man ??!</div>');
        alert(obj.html());
    });
});
Run Code Online (Sandbox Code Playgroud)

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

但,

我得到旧内容而不是......

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

我打算访问新的'obj'

$('.selector').click(function(){
            var obj = $(this),
            repl = $('<div class="size">whats up man ??! <span class="medium"></span></div>');
            obj.replaceWith(repl);
            alert(obj.find('span').attr('class')); //this will print 'undefined'
});
Run Code Online (Sandbox Code Playgroud)

我想要打印'span'的类名称'medium'

Nic*_*ver 10

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

$('.selector').click(function(){
  var obj = $(this),
      repl = $('<div class="size">whats up man ??! <span class="medium"></span></div>');
  obj.replaceWith(repl);
  alert(repl.find('span').attr('class')); 
});
Run Code Online (Sandbox Code Playgroud)

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