如何获取上一个兄弟的名字 [英] How to get the Previous Sibling name
本文介绍了如何获取上一个兄弟的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< html>
< head>
< script type =text / javascript>
function myFunction()
{
var itm = document.getElementById(item2);
alert(itm.previousSibling.name);
}
< / script>
< / head>
< body>
< p name ='pn'>段< / p>
< button id ='item2'onclick =myFunction()>尝试它< / button>
< / body>
< / html>
编辑:
code>< table id =sort>
< tr name =nodrag nodrop>
< td colspan = 3>< strong>< a style =cursor:pointer; class =toggle>组1< / a>< / strong> < / TD>
< td style =text-align:right;>< a class =button3href =#>< span>编辑< / span>< / a> < a class =button3href =#>< span>删除< / span>< / a>< / td>
< / tr>
< tr id =1class =tr_group'>
< td style =width:10px;类= dragHandle >&安培; NBSP;< / TD>
< td>< a href =#style =margin-left:20px;> Umair Iqbal< / a>< / td>
< td>< span style =font-size:12px; color:#999; line-height:100%;> TUM的学生< / span>< / td>
< td style =text-align:right;>< a class =button3href =#>< span>编辑< / span>< / a> < a class =button3href =#>< span>删除< / span>< / a>< / td>
< / tr>
Ist行是第二行的前一个兄弟。我想要第一行的名字,我所有的ids将是动态的
谢谢
解决方案
使用jQuery将是:
$('#item2')prev()。attr name);
需要使用以下函数(以确保忽略空白节点)
getPreviousSiblingName(document.getElementById(item2))
函数getpreviousSiblingName(element){
var p = element;
do p = p.previousSibling;
while(p&& p.nodeType!= 1);
return p.attributes [name]。value;
}
I need to get the name of the previous sibling . to keep it simple i have some sample code
<html>
<head>
<script type="text/javascript">
function myFunction()
{
var itm=document.getElementById("item2");
alert(itm.previousSibling.name);
}
</script>
</head>
<body>
<p name='pn'>paragraph</p>
<button id='item2' onclick="myFunction()">Try it</button>
</body>
</html>
Edit:
<table id="sort">
<tr name="nodrag nodrop">
<td colspan=3><strong><a style="cursor:pointer;" class="toggle">Group 1</a></strong> </td>
<td style="text-align: right;"><a class="button3" href="#" ><span> Edit </span></a> <a class="button3" href="#" ><span> Delete </span></a></td>
</tr>
<tr id="1" class="tr_group"'>
<td style="width:10px;" class="dragHandle"> </td>
<td><a href=# style="margin-left: 20px;">Umair Iqbal</a></td>
<td><span style="font-size: 12px; color: #999; line-height: 100%;">A Student at TUM</span></td>
<td style="text-align: right;"><a class="button3" href="#" ><span> Edit </span></a> <a class="button3" href="#" ><span> Delete </span></a></td>
</tr>
The Ist row is the previous sibling of the second row. I want the name of the 1st row and all my ids will be dynamic
thanks
解决方案
Using jQuery it would be:
$('#item2').prev().attr("name");
With regular javascript you would need to use the following function (to ensure whitespace nodes are ignored)
getPreviousSiblingName(document.getElementById("item2"))
function getpreviousSiblingName(element) {
var p = element;
do p = p.previousSibling;
while (p && p.nodeType != 1);
return p.attributes["name"].value;
}
这篇关于如何获取上一个兄弟的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文