I would like to use JavaScript to insert a new li element into an existing ul. The new li element would be the last item in the ul.
<div id="foo">
<div>
<ul>
<li></li>
<li></li>
....insert html here....
</ul>
</div>
</div>
Should be as easy as
var ul = document.querySelector('#foo ul');
var li = document.createElement('li');
li.innerHTML = 'new LI';
li.className = 'someclass';
ul.appendChild(li);
or in jQuery
$('#foo ul').append( $('<li />', {text : 'new LI'}) );
Try ".append()"
$('#foo ul').append( '<li />' );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="foo"> <div> <ul> <li></li> <li></li> ....insert html here.... </ul> </div> </div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.