1 回答
TA贡献1982条经验 获得超2个赞
您可以使用this来引用单击的按钮,.parentElement向上爬,然后选择段落及其内部文本。可以从那里按照您喜欢的方式解析文本。
var calHosting = document.querySelectorAll('.calculate-hosting');
[...calHosting].forEach(cal => {
cal.addEventListener('click', function (event) {
var paragraphs = this.parentElement.querySelectorAll("p");
var p1 = paragraphs[0].innerText;
var p2 = paragraphs[1].innerText;
var price = Number(p2.match(/\d+\.\d+/)[0]);
console.log(p1); // Nebula
console.log(p2); // Price1.44 /mo
console.log(price); // 1.44
// document.getElementById('cal-container').style.display = 'block';
});
});
<div class="item__top">
<p> Nebula</p>
<p>
Price<span class="big-price">1</span>.44
<span class="text-large">/mo</span>
</p>
<p>$2.88</p>
<button class="calculate-hosting">Get Started</button>
<p>You pay $17.28 — Renews at $33.88/year</p>
</div>
添加回答
举报