Mô tả
Trích xuất text từ thẻ span có class "purchase amount", chuyển sang số, nhân 10, hiển thị kết quả vào thẻ p#demo.
Code HTML + JavaScript/jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<body>
<tr>
<th>Value=</th>
<td><strong><span class="purchase amount"><span></span>10</span></strong></td>
<p id="demo"></p>
</tr>
</body>
<script>
$(document).ready(function() {
var v = parseInt($(".purchase.amount").text().trim().replace(',', ''));
var totalValue = v * 10;
console.log(totalValue);
$("#demo").text("After multiplying the Value 10=" + totalValue);
});
</script>
</html>
Giải thích
$(".purchase.amount")chọn span có cả 2 class.text()lấy text content.trim().replace(',', '')làm sạchparseInt()chuyển sang số$("#demo").text(...)gán text cho p#demo
Vanilla JS (không jQuery)
const span = document.querySelector('.purchase.amount');
const value = parseInt(span.textContent.trim().replace(',', ''));
document.getElementById('demo').textContent = `After multiplying the Value 10=${value * 10}`;