2017-07-28 95 views
3

我有一個html結構像這樣的網站上,我正在寫一個腳本(js上Tampermonkey),這需要找回這是下表「在一路走來」但有下的那些‘在回來的路上’,所以我不知道我能做到這一點有一個聰明的辦法如何獲得表它們是經過特別的頭銜,

我有代碼來顯示,因爲我的想法這一點,也許有一些jQuery選擇,這將有助於我卜我不知道這一切,並在文檔還沒有找到

這只是由2 <h4>,然後它可以在0和X臺之間有,沒有id存在除了在頂部的跨度,並

<span id="idid"> 
 

 
      <h4 class="spacer">On way go :</h4> 
 
      <table class="traders" cellpadding="1" cellspacing="1"> <!-- need this --> 
 
     \t  <thead><!-- --></thead> 
 
     \t  <tbody><!-- --></tbody> 
 
     </table> \t 
 
      <table class="traders" cellpadding="1" cellspacing="1"> <!-- need this --> 
 
     \t  <thead><!-- --></thead> 
 
     \t  <tbody><!-- --></tbody> 
 
     </table> \t 
 
      
 
      <h4 class="spacer">On way back</h4> 
 
      <table class="traders" cellpadding="1" cellspacing="1"> <!-- not this --> 
 
     \t  <thead><!-- --></thead> 
 
     \t  <tbody><!-- --></tbody> 
 
     </table> \t 
 
     <table class="traders" cellpadding="1" cellspacing="1"> <!-- not this --> 
 
     \t  <thead><!-- --></thead> 
 
     \t  <tbody><!-- --></tbody> 
 
     </table> \t 
 
    </span>

提示表之間沒有識別符號:不我的網站,在結構上沒有控制,我跟那

+0

理想情況下,你應該把在單獨的容器中的div表來組織它們。在路上去做一個div,在另一個路上去做另一個div。然後你可以得到一個div的孩子(排除所有其他元素)。 – clabe45

+0

@ clabe45 OP在Tampermonkey是這樣做的所以大概OP desn't對HTML –

+0

是當然的任何控制,這將是完美的,我根本就不會在這裏問(我當然知道一些事情),但是......網站不是我的,所以對 – azro

回答

1

你可以使用document.evaluate,搜索使用xpath得到的跨度,然後使用nextElementSibling拿到表格文字工作:

let e = document.evaluate("//h4[contains(., 'On way go')]", document, null, XPathResult.ANY_TYPE, null); 
 
let first = e.iterateNext(); 
 
let el = first.nextElementSibling; 
 
while (el.tagName.toUpperCase() === 'TABLE') { 
 
    console.log(el); 
 
    el = el.nextElementSibling; 
 
}
<span id="idid"> 
 

 
      <h4 class="spacer">On way go :</h4> 
 
      <table class="traders" cellpadding="1" cellspacing="1"> <!-- need this --> 
 
     \t  <thead><!-- --></thead> 
 
     \t  <tbody><!-- --></tbody> 
 
     </table> \t 
 
      <table class="traders" cellpadding="1" cellspacing="1"> <!-- need this --> 
 
     \t  <thead><!-- --></thead> 
 
     \t  <tbody><!-- --></tbody> 
 
     </table> \t 
 
      
 
      <h4 class="spacer">On way back</h4> 
 
      <table class="traders" cellpadding="1" cellspacing="1"> <!-- not this --> 
 
     \t  <thead><!-- --></thead> 
 
     \t  <tbody><!-- --></tbody> 
 
     </table> \t 
 
     <table class="traders" cellpadding="1" cellspacing="1"> <!-- not this --> 
 
     \t  <thead><!-- --></thead> 
 
     \t  <tbody><!-- --></tbody> 
 
     </table> \t 
 
    </span>

如果您知道該文本將完全On way go :,你也可以使用"//h4[text()='On way go :']"作爲XPath表達式。

相關問題