I have this html:
<span class="price-amount amount"><span class="price-currencySymbol">£</span>49.99</span>
I want to extract just the value of '49.99' without the html and currency symbol.
I have tried this:
function() {
var element = document.querySelector('.price-amount');
var price = element.innerHTML.replace('£', '');
return price;
}
But the result is this:
<span class="price-currencySymbol">£</span>49.99
Rather than: 49.99
By using selector logic and accessing the proper text node you don't even need to use regexp.
var price = document.querySelector(".price-amount") .childNodes[1].textContent; console.log(`Extracted price: ${price}`);
<span class="price-amount amount"><span class="price-currencySymbol">£</span>49.99</span>
如果您想在每种情况下都删除'.price-currencySymbol'
跨度中的内容,则只需清空其html:
document.querySelector(".price-amount").innerHTML = "";
Try this
var price = element.innerHTML.match(/\d+\.\d+/)[0];
where
.match
searches the pattern of price using regex.
[0]
returns the first match, and in your case it would be the only match.
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.