简体   繁体   中英

How to get text sibling of element using jQuery?

I am scraping a site and I found this

<table>
  <tr>
    <td>
      <b>Status:</b>ACTIVE;
      <b>Type:</b>CN - CONSTRUCTION
      <b>Added:</b>02/24/2012
    </td>
  </tr>
</table>

How do I get status , type , and added individually?

I know I will get downvotes because I am not posting any TRIED CODE... but I cant even seem to think what to try!

This website has POOR HTML structure and I cant seem to find any way.

  • Use jQueryElement.text() to grab all the text.
  • Use String#spplit to split the string

 var text = $('#content').text(); var split = text.trim().split('\\n'); split.forEach(function(el) { var splitAgain = el.split(':'); console.log("Key: " + splitAgain[0].trim() + " Value: " + splitAgain[1].trim()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <table> <tr> <td id="content"> <b>Status:</b>ACTIVE; <b>Type:</b>CN - CONSTRUCTION <b>Added:</b>02/24/2012 </td> </tr> </table> 

Javascript nextSibling property get next text sibling of element. You can select b elements in td and get next text of it.

 $("td > b").each(function(){ console.log(this.innerText +" = "+ this.nextSibling.nodeValue.trim()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <b>Status:</b>ACTIVE; <b>Type:</b>CN - CONSTRUCTION <b>Added:</b>02/24/2012 </td> </tr> </table> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM