问一下原生的js中如何得到td在tr中的索引吗?类似jQuery中的index()方法这样的?

<html>

<body>

<table>

<tr>

<td id="a">1</td>

<td>2</td>

<td>3</td>

<td>4</td>

<td>5</td>

</tr>

</table>

<body>

<script src='http://172.29.5.201/assets/js/core/jquery.min.js'></script>

<script>

alert($("#a").index());</script>

</html>


繁华开满天机
浏览 1121回答 3
3回答

呼如林

function searchIndex(id){&nbsp; &nbsp; //获取 td&nbsp; &nbsp; var td = document.getElementById("td");&nbsp; &nbsp; //获取所有的 td&nbsp; &nbsp; var arr = Array.from(td.parentNode.children);&nbsp; &nbsp; //遍历&nbsp; &nbsp; arr = arr.filter(function(item){&nbsp; &nbsp; &nbsp; &nbsp; return td == item;&nbsp; &nbsp; });}

慕容708150

document.getElementById('a').cellIndex
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript