I want a certain div to be added as the last element in a list no matter what. Is there any way in which I can explicitly specify this?
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Include empty value fields in jQuery .serialize()
- Disable Browser onUnload on certain links?
- how to get selected text from iframe with javascri
If you want to absolutely make sure of that, place empty container with an ID and replace content inside if you need to up date it, so if you have a big container and then sub-containers within, the last child should be like so
HTML
now if you need to update,
jQuery
Now, if you wish to keep the old content from last child div and insert new below that you can do following
Or, if you want to keep old content in last child div but have new content be displayed on top or before the old, you can just swap the order of var newcontent like so
Get the parent of the list and add the new item to that parent as the last child.
Using plain javascript:
Mozilla documentation: https://developer.mozilla.org/En/DOM/Node.appendChild
If you want to add other items and still have this item be the last item, then you will have to add the new items before this last item or remove this item, append your new item, then append this one back again at the end.
Once you already have the last element in the list, if you then want to add a new element right before that last element, you can do it like this:
$('#list').append('<div></div>')
will append it to the very end of the #listIf you want to append it to the very last div, just in-case there are other elements after that, then you can use
$('#list div:last').after('<div></div>')
When you are talking about list, I will assume you are referring to un-ordered lists.
You will need to so something of this sort: