2016-06-10 36 views
-1

如何在沒有任何類的時候報廢數據我知道使用ID的類,使用document.getElementsByClassName的類。如何刮沒有使用javascript的類

<tr id="overview-summary-current"> 
 
    <th scope="row"> 
 
     <span class="edit-tools"> 
 
     <a href="#background-experience" class="edit-section" id="control_gen_5">Edit experience 
 
     </a> 
 
     </span> 
 
     <a href="#background-experience" data-trk="prof-0-ovw-curr_pos">Current</a> 
 
    </th> 
 
    <td> 
 
     <ol> 
 
     <li> 
 
      <strong> 
 
      <a href="/company/10097785?trk=prof-0-ovw-curr_pos" dir="auto">SNT Solutions rajkot</a> 
 
      </strong> 
 
     </li> 
 
     </ol> 
 
    </td> 
 
</tr>

我想SNT Solutions rajkot如何使用JavaScript獲得變量值。

trk=prof-0-ovw-curr_pos在網頁獨特

+1

使用jQuery/Cheerio它簡單的解決方案很容易:'$(「a [href^='/ company'」])。text()',但在純JavaScript中,這是一個痛苦。你必須選擇所有'a'標籤,然後過濾它們和東西。 –

+0

任何使用純JavaScript的方法@JeremyThille –

+0

好吧,如果你在刮,它意味着你正在運行JavaScript服務器端,所以你使用的是Node(或者我缺少的東西)。所以只需要'npm install cheerio'並享受服務器端的jQuery :)爲什麼要麻煩? –

回答

0

我爲

var a1 = document.querySelector('a[href$="trk=prof-0-ovw-curr_pos"]').text; 
 
console.log('Data is :::::'+a1);

2

簡單的方式(純JS):

var linksWithoutClass = document.querySelectorAll('#overview-summary-current a'); // return nodeList iterrator 

var linksArray = [].slice.call(linksWithoutClass); // create array from Nodelist 

linksArray.filter(function(link){ // filter list to get <a> without any class 
    return link.classList.length === 0; 
}); 
+0

我想純JS或jQuery代碼不是其他@JulesGoullee –

+1

Hummm ....我的例子已經在純js [...] –

+0

oppps我明白了,但是如何得到'SNT Solutions rajkot'? –