how to get actual content of an object after being replaceWith('something') in jquery

后端 未结 2 716
别跟我提以往
别跟我提以往 2021-01-11 17:44

I have this code

$(document).ready(function(){
    $(\'.selector\').click(function(){
        obj = $(this);
        obj.replaceWith(\'
相关标签:
2条回答
  • 2021-01-11 18:09

    Your updated method is the correct one, just needs a tweak like this:

    $('.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')); 
    });
    

    You can test it out here. The important change is the repl.find() to look in the new element instead of the old one.

    0 讨论(0)
  • 2021-01-11 18:23

    why do you want to do that? you know what you replaced it with....

    $(document).ready(function(){
        $('.selector').click(function(){
            var obj = $(this);
            var replacement = $('<div class="size">whats up man ??!</div>');
            obj.replaceWith(replacement);
            alert(replacement.html());
        });
    });
    
    0 讨论(0)
提交回复
热议问题