Селекторы jQuery - выбор текста в определенном теге
Вот мой фрагмент кода:
<div class="totals">
<table id="shopping-cart-totals-table">
<col />
<col width="1" />
<tfoot>
<tr>
<td style="" class="a-right" colspan="1">
<strong>Grand Total</strong>
</td>
<td style="" class="a-right">
<strong><span class="price">$364.99</span></strong>
</td>
</tr>
</tfoot>
<tbody>
<tr>
<td style="" class="a-right" colspan="1">
Subtotal </td>
<td style="" class="a-right">
<span class="price">$354.99</span> </td>
</tr>
<tr>
<td style="" class="a-right" colspan="1">
Shipping & Handling (Flat Rate - Fixed) </td>
<td style="" class="a-right">
<span class="price">$10.00</span> </td>
</tr>
</tbody>
</table>
Как я могу использовать jQuery для выбора первого диапазона класса "цена" и присвоить переменную "$ 364.99" в этом теге?
var total = $('.price:first').text();
Вы можете сделать следующее:
var price = $('span.price').first().text();
Или любой из них:
var price = $('span.price:first').text();
var price = $('span.price:eq(0)').text();
var price = $('span.price').eq(0).text();
var price = $($('span.price').get(0)).text();
var price = $($('span.price').get()[0]).text();
Чтобы получить текст
$(".price").eq(0).text()
Чтобы установить текст
$(".price").eq(0).text('12.21$')
- Вопросы
- Css-selectors
- Селекторы jQuery - выбор текста в определенном теге