如何获取元素的位置号?
firefox 中是否有任何插件或 firebug 中的函数或其他东西可以让我设置特定元素的位置号?
例如,如果我想知道特定 TD 元素与文档中所有 TD 相比的位置。
示例:
<html>
<head>
<body>
<table>
<tr>
<td></td> (0)
<td></td> (1)
<td></td> (2)
<td></td> (3)
<td></td> (And so on...)
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td> <------ WHAT IS THE POSITION NUMBER OF THIS TD?
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>
firefox 中的 Web 开发人员工具栏有一个工具,可让您查看所有 DIV 的索引号。这是我需要的,但对于其他元素,而不仅仅是 DIV。
PS。我受到正确答案的启发,我提出了自己的解决方案:
var dom = document.getElementsByTagName('td');
var x;
for(x = 0; x < dom.length; x++)
{
dom[x].innerHTML = dom[x].nodeName + '[' + x + '] ' + '(' + dom[x].innerHTML + ')';
dom[x].style.color = 'blue';
}
Is there any plugin in firefox or function in firebug or something else, that lets me se the position number of a specific element?
If i for example want to know the what position a specific TD element has compared to all the TD's in the document.
EXAMPLE:
<html>
<head>
<body>
<table>
<tr>
<td></td> (0)
<td></td> (1)
<td></td> (2)
<td></td> (3)
<td></td> (And so on...)
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td> <------ WHAT IS THE POSITION NUMBER OF THIS TD?
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>
The webdeveloper toolbar in firefox has a tool that lets you see the index number of all DIV's. This is what i need but for other elements, not just DIV.
PS. I was inspired by the correct answer and i made my own solution:
var dom = document.getElementsByTagName('td');
var x;
for(x = 0; x < dom.length; x++)
{
dom[x].innerHTML = dom[x].nodeName + '[' + x + '] ' + '(' + dom[x].innerHTML + ')';
dom[x].style.color = 'blue';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,以下内容将满足您的需要:
JS Fiddle 演示。
编辑上面的内容是为了展示如何将元素的索引(在其同级元素中)分配给变量:
编辑以响应OP在其他答案上留下的评论:
引用。
以下函数将获取并返回文档中相同类型的其他标签中被单击元素的索引位置(实际上比上面更容易):
JS Fiddle 演示。
参考文献:
childNodes
。e.preventDefault()
。e.stopPropagation()
。getElementsByTagName()
。parentNode
。push()
。Well, the following will do as you need:
JS Fiddle demo.
Edited the above in order to show how to assign the element's index, amongst its siblings, to a variable:
Edited in response to comments left on other answers, by OP:
Citation.
The following function will get, and return, the index position of the clicked element amongst other tags of the same type in the document (which is actually somewhat easier than the above):
JS Fiddle demo.
References:
childNodes
.e.preventDefault()
.e.stopPropagation()
.getElementsByTagName()
.parentNode
.push()
.多姆树。如果您尝试通过 XPath 获取每个元素都有编号。
Dom-tree. Every element have number if you try to get it by their XPath.
编辑
抱歉,我误解了你的问题。这就是答案:
像这样:
使用 javascript,您可以获得 tds 的数量。
上面的示例将打印文档中 td 元素的总数。但是,如果您想要特定的 td,则应该为该 td 提供一个标识符。例如,假设您的 html 文件如下:
现在使用 javascript,您可以:
此示例将给出您的 td 位置。
EDIT
Sorry, I've misunderstood your question. Here is the answer then:
Like this:
With javascript you can get the numbers of tds.
The example above will print the totoal number of td elements in your document. However, if you want a particular td, you should give that td an identifier. For instance assume your html file is like:
Now with javascript you can:
This example will give your td's position.