我知道我能做到
$('.intro').append('<p>this is my text</p>');
但是我如何通过链接它向新元素发送文本
$('.intro').append('<p></p>').text('this is my text');
最佳答案
.append()
调用返回运行它的元素(代码中的 .intro
元素),而不是 append 元素(p
在你的代码中)。
如果您愿意,可以将 p
元素的创建和 append 分开,如下所示:
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...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="intro"></div>
关于Jquery 将文本添加到新的 append 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42252581/