Jquery 将文本添加到新的 append 元素

标签 jquery text append

我知道我能做到

  $('.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/

相关文章:

javascript - dataTables 标题对齐问题

text - 如何从文本文件中删除不以某些字符开头的行(sed 或 grep)

javascript - 在 javascript 中将 HTML 元素 append 到当前元素

arrays - 在Perl6中将多个值 append 到数组

jQuery 删除除 anchor 之外的所有 HTML 标签

javascript - 这些事件处理程序中的哪一个应该在窗口关闭时触发自定义函数?

阻止父操作的 Javascript 弹出打印页面

java - Java中如何读取和分割txt文件的内容?

text - 如何在 Notepad++ 中显示列号?

c++ - 构造以特定前缀开头的特定长度的字符串