[英]How to get first and last element <li>?
I would like to get first and last element in <li>
: 我想在
<li>
获得第一个和最后一个元素:
<script>
function editMediaInfo(mediaID){
var x = document.getElementsByTagName("li")[0]
alert(x);
}
</script>
<?php
$x = 1;
echo "<ol>";
while($x <= 5){
echo "<li id={$x}> item {$x} </li>";
?>
<button onclick="editMediaInfo('<?php echo $x; ?>')">Click</button>
<?php
$x++;
}
echo "</ol>";
?>
When I click the button, I'd like to get the first and the last element in <li>
. 当我单击按钮时,我想获取
<li>
的第一个和最后一个元素。 How can I do that? 我怎样才能做到这一点?
You can try this way with lastChild
property 您可以使用
lastChild
属性尝试这种方式
var x = document.getElementsByTagName("ul")[0];
var lastchild = x.lastChild;
alert(lastchild);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.