How to get sibling text after element using jquery

2019-07-20 03:22发布

问题:

I am trying to get value after the bold close tag using html dom. can anybody know how to get these values using dom 8/14/15, 7333, MULTIPURPOSE, RAILROADED etc. I can get values in bold tag but how i get after the bold tag text.

<div>       
   <b>Intro Date:</b> 8/14/15               
   <br><b>SKU#:</b> 7333                
   <br><b>Use:</b> MULTIPURPOSE             
   <br><b>Direction:</b> RAILROADED             
   <br><b>Width:</b> 54"                
   <br><b>Horiz Repeat:</b> 6.75                
   <br><b>Vert Repeat:</b> 0.0      
</div>

回答1:

Use javascript nextSibling property to getting text sibling after element.

$("b").each(function(){
    console.log(this.nextSibling.nodeValue.trim());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>       
   <b>Intro Date:</b> 8/14/15               
   <br><b>SKU#:</b> 7333                
   <br><b>Use:</b> MULTIPURPOSE             
   <br><b>Direction:</b> RAILROADED             
   <br><b>Width:</b> 54"                
   <br><b>Horiz Repeat:</b> 6.75                
   <br><b>Vert Repeat:</b> 0.0      
</div>