I have a handle on an Un-ordered List (for my example i will call the handle Var1) and would like to be able to assign its last li to a variable. I tried Lastli = var1.lastChild
the only method I figured would work but it didn't. I can't seem to find a answer to this using only Javascript not jQuery any help is appreciated.
标签:
javascript
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
you can select all 'li' and take the last one. Something like:
either
ulReference.children[ulReference.children.length -1]
orulReference.childNodes[ulReference.childNodes.length -1]
. The difference between the two can be found hereLets consider an example
To get the last child of the list you can simply make use of queryselector
Suppose you want the n-th child for that you can use the following syntax:
If you want all the list item in the given list:
Try this:
.childNodes[childNodes.length - 1]
You can select the parent element and use the lastChild property.
Or you select all the items into an array and get the last item. Here is a quick example: