如何将元素的文本修改为自己的文本以及JQuery中的额外文本?

时间:2011-02-28 05:06:41

标签: jquery

E.g:

<div class="cloneMyself">clone my self and plus more text</div>
<div class="clickMe">add more info to cloneMyself</div>

$('.clickMe').click(function(){

    $('.cloneMyself').text(// here I want it to be clonMyself's text plus extra info);
});

然后当我点击clickMe时,我需要cloneMyself's文字:

原文+新文字

clone my self and plus more text from here was added after click it.

非常感谢!!

2 个答案:

答案 0 :(得分:0)

您可以使用回调(请参阅docs获取信息)查询当前元素的.text()

$('.clickMe').click(function() {
    $('.cloneMyself').text(function() {
        return $(this).text() + " extra info";
    });
});

答案 1 :(得分:0)

试试这个:

$('.clickMe').click(function(){

    $('.cloneMyself').text($('.cloneMyself').text() + "some extra info");
});