简体   繁体   English

如何在使用 JavaScript 发生点击事件时捕获 p 元素值

[英]how to capture p elements value when click event happened using JavaScript

Here is my code.这是我的代码。 I want to capture Nebula and Price 1.44 when I click on button.我想在单击按钮时捕获星云和价格 1.44。 Can anyone help me please.任何人都可以请帮助我。

Html code html代码

           <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>

JS code JS代码

var calHosting = document.querySelectorAll('.calculate-hosting');
[...calHosting].forEach(cal=>{
    cal.addEventListener('click',function(event){

        document.getElementById('cal-container').style.display='block';

    });
});

Note: I am dealing with mutiple buttons so thats why I sued querySelectorAll.

You can use this to refer to the button that was clicked, .parentElement to climb up, and then select paragraphs and their inner text.您可以使用this来引用被单击的按钮, .parentElement向上爬,然后选择段落及其内部文本。 The text can be parsed however you like from there.可以根据需要从那里解析文本。

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何向 iframe 中的 p 元素添加点击事件(使用 jQuery) - How to add a click event to p elements in iframe (using jQuery) 如何使用带有香草Javascript的部分ID捕获元素的点击? - How to capture a click on elements using partial IDs with vanilla Javascript? 标记未触发时,点击事件。 如何捕捉事件 - On Click event when tag clicked on not firing. How to capture event 使用jQuery forEach查找 <p> 发生原始点击的位置 - using jQuery forEach to find the <p> on which the original click happened 如何使用JavaScript在弹出窗口中捕获事件? - How to capture the event in the popup window using javascript? 如何使用javascript捕获浏览器刷新事件 - How to capture browser refresh event using javascript 如何使用 Javascript 捕获输入更改事件? - How to capture input changes event using Javascript? 如何在下拉列表中获取所选值 <div><p> 使用点击事件的jQuery? - How to get the selected value on a Dropdown of <div> <p> using JQuery by click event? 如何在 JavaScript 上捕获双击鼠标中键(滚轮)事件? - How to capture the double click mouse middle button(wheel) event on JavaScript? 如何正确捕获JavaScript点击事件来隐藏和显示一个div? - How to properly Capture JavaScript Click event to Hide and Show a Div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM