简体   繁体   中英

How to get text of the span element within anchor tag?

I was just wonder about how to get the text of the span element within the anchor tag.

 <a class="draw-ticket-wrap"> <span style="display: none;" class="TicketValue">1000</span> <span style="display: none;" class="TicketId">7</span> <span style="display: none;" class="TicketTotalNumberOfTickets">125</span> <span style="display: none;" class="TicketTitle">$1,000 Order</span> <span style="display: none;" class="TicketDescription"><p><strong>200 Chances to win</strong> </a> 

I would like to get the text of the first span element whose class name is 'TicketValue'. The entire html is a jquery object ie $html.

var text = $(".TicketValue").text();

There are many ways to get the text of the first span. Below uses span's class selector to select the first span. See below,

 $(function() { alert($('a.draw-ticket-wrap span.TicketValue').text()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a class="draw-ticket-wrap"> <span style="display: none;" class="TicketValue">1000</span> <span style="display: none;" class="TicketId">7</span> <span style="display: none;" class="TicketTotalNumberOfTickets">125</span> <span style="display: none;" class="TicketTitle">$1,000 Order</span> <span style="display: none;" class="TicketDescription"><p><strong>200 Chances to win</strong> </p></span> </a> 

这是根据您的条件的另一种方法

$html.find('.TicketValue')[0].innerText

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