2016-11-10 33 views
-1

对于缺少代码感到抱歉,但我只需要有人指点道路并帮助我使用each()语句来提醒每个TR的ID。使用jQuery获取每个表格行的ID

$(document).ready(function() { 
 

 
    /* alert each TR's ID from #theTable */ 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<table id="theTable"> 
 
    <tr id="id1"></tr> 
 
    <tr id="id2" class="theClass"></tr> 
 
    <tr id="id3"></tr> 
 
</table>

+0

这个t问题的类型可以轻松找到! –

回答

3

使用each()方法,并得到该元素的id财产。

$(document).ready(function() { 
 
    $('#theTable tr').each(function() { 
 
    console.log(this.id) 
 
    }) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<table id="theTable"> 
 
    <tr id="id1"></tr> 
 
    <tr id="id2" class="theClass"></tr> 
 
    <tr id="id3"></tr> 
 
</table>

+1

我喜欢这个,特别是'this.id'。奇怪的* attr *在* prop *已经存在了很久之后仍然在使用(但都不是必需的)。 ;-) – RobG

1

在这里你去:

$("#theTable tr").each(function(){ 
    alert($(this).attr("id")); 
}); 
1

希望这将帮助你:)

$(function() { 
 

 
    $("#theTable tr").each(function() { 
 
     console.log($(this).attr('id')) // Here you go 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="theTable"> 
 
    <tr id="id1"></tr> 
 
    <tr id="id2" class="theClass"></tr> 
 
    <tr id="id3"></tr> 
 
</table>

+0

你的回答和Pranav C Balan给出的答案有什么不同? –

1
$(document).ready(function(){ 
     $("#theTable").find("tr").each(function(){ 
      console.log($(this).attr("id")); 
     }); 
    }); 

希望这可以帮助全

1

$(function() { 
 

 
    $("#theTable tr").each(function() { 
 
    alert($(this).attr('id')); // Here you go 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="theTable"> 
 
    <tr id="id1"></tr> 
 
    <tr id="id2" class="theClass"></tr> 
 
    <tr id="id3"></tr> 
 
</table>
尝试其排在警报

1

通过使用每个for循环和通过推添加ID来排列。

$(document).ready(function() { 
 
    var ids=[]; 
 
    $('#theTable tr').each(function() { 
 
    ids.push(this.id) 
 
    }) 
 
alert(ids + '  Use your id'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<table id="theTable"> 
 
    <tr id="id1"></tr> 
 
    <tr id="id2" class="theClass"></tr> 
 
    <tr id="id3"></tr> 
 
</table>

1

下面是代码。

.attr('id').prop('id')都可以为您的目的获取<tr>的编号。

$(document).ready(function() { 
 

 
    /* alert each TR's ID from #theTable */ 
 
    $("#theTable tr").each(function() {  
 
    console.log($(this).attr('id')); 
 
    
 
    alert($(this).attr('id')); 
 
    
 
    // prop() can also be used to get the id 
 
    // console.log($(this).prop('id')); 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<table id="theTable"> 
 
    <tr id="id1"></tr> 
 
    <tr id="id2" class="theClass"></tr> 
 
    <tr id="id3"></tr> 
 
</table>

1

很多jQuery的解决方案,为后人这可以以纯JS使用querySelectorAllArray.prototype.forEach完成:

window.onload = function(){ 
 
    [].forEach.call(document.querySelectorAll('#theTable tr'), x => console.log(x.id)); 
 
};
<table id="theTable"> 
 
    <tr id="id1"></tr> 
 
    <tr id="id2" class="theClass"></tr> 
 
    <tr id="id3"></tr> 
 
</table>