JQuery有很多插入节点的方法,append ,appendTo和after,prepend, preppendTo,before。以及insertAfter和insertBefore。insertAfter和insertBefore和appendTo,prependTo类似这边就不赘述了。同样为了节约篇幅,只探讨往后插入节点 append ,appendTo和after。
append 将节点内容插入到指定的段落内。
<p>desmond</p>
$("p").append("<span>你好</span>");
运行后的结果为desmond你好
appendTo 其实是append的反过来,把定义好的内容追加到指定的标签中去。
<p>desmond</p>
$("<span>你好呀</span>").appendTo(“p”);
运行后的结果为desmond你好after 将节点添加到指定的标签的后面,感觉上类似换行。
<p>desmond</p>
$("p").after("<span>你好</span>");
运行后的结果为desmond
你好