2016-11-10 32 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>