LeB*_*eau 3 jquery text append
我知道我能做到这一点
$('.intro').append('<p>this is my text</p>');
Run Code Online (Sandbox Code Playgroud)
但是如何通过链接将新文本发送到新元素
$('.intro').append('<p></p>').text('this is my text');
Run Code Online (Sandbox Code Playgroud)
该.append()调用返回运行它的.intro元素(代码中的元素),而不是附加元素(p在代码中).如果你想要你可以分开创建p元素的附加,如下所示:
var $p = $('<p />').append('your text goes here');
// and then you can:
$('.intro').append($p); // this returns the '.intro' element
// or
$p.appendTo($('.intro')); // this returns the $p element...Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="intro"></div>Run Code Online (Sandbox Code Playgroud)