2015-09-03 31 views
3

以下是我與合作:查找H3標籤之後的下一個表

<table class="info-table"></table> 
<h3><span id="regular_text">Regular Text</span></h3> 
<table class="info-table"></table> <!--Give this table an id.--> 

我需要給第二個表中的ID,但我不知道我怎樣才能達到目標。是否有某種jQuery或JavaScript可以在h3 #regular_text之後計算「next」表?

+0

你想對這張桌子做什麼? –

回答

3

使用adjacent sibling selector

document.querySelector('h3 + table'); 

如果您需要選擇包含了ID regular_text一個元素h3標籤後會立即表,我不知道一個純CSS選擇器的解決方案,但你可以這樣做在普通的JS像這樣:

var heading = document.getElementById('regular_text').parentElement; 
var table = heading.nextElementSibling; 
table.id = 'tableId'; 
+1

這是最好的,但是這個描述聽起來像是他們需要考慮'#regular_text',而這當前不能在本地選擇器中完成。 –

+1

@squint正確。我更新了答案。 – GolfWolf