如何在jQuery中被替换为('something')后获取对象的实际内容 [英] how to get actual content of an object after being replaceWith('something') in jquery

查看:111
本文介绍了如何在jQuery中被替换为('something')后获取对象的实际内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

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

我想获取已经是'replaceWith'的'obj'的新内容

I want to get the new content of 'obj' that has been 'replaceWith'

但是

我得到的是旧内容...

I get the old content instead ...

如何获取'obj'的实际内容?

how do I get the actual content of 'obj' ???

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

my intention is to access the new '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'
});

我要打印"span"的类名,即"medium"

I want to print the class name of the 'span' which is 'medium'

推荐答案

您更新的方法是正确的,只需进行如下调整:

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

您可以在此处进行测试.重要的更改是repl.find()可以查找新元素,而不是旧元素.

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

这篇关于如何在jQuery中被替换为('something')后获取对象的实际内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆