简体   繁体   English

如何使用jQuery或Javascript添加元素

[英]How to add an element with jQuery or Javascript

I want to know how to add an HTML element with jQuery or Javascript after another element 我想知道如何在另一个元素之后添加带有jQuery或Javascript的HTML元素

for example, from this 例如,从这里

<ul>
<li> .... </li>
<li> .... </li>
<li> .... </li>
</ul>

to this 对此

<ul>
<li> .... </li>
<div>..</div>
<li> .... </li>
<div>..</div>
<li> .... </li>
<div>..</div>
</ul>

You can not append a div as a child of a UL so no you can not add the div element after the li . 您不能将div作为UL的子级追加,因此不能在li之后添加div元素。 The browser will "fix" your bad HTML the best as it can and not every browser will do the same thing. 浏览器将尽其所能“修复”您的不良HTML,并且并非每个浏览器都会做同样的事情。

If you append and li that has a div as a child, it will work correctly. 如果您追加并且有一个div作为孩子的li ,它将正常工作。

 $("li").after("<li><div>New</div></li>"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li> .... </li> <li> .... </li> <li> .... </li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM