[英]How to get the text value in a div html by clicking on button
I want to get the text between the div tag by clicking on the button event我想通过单击按钮事件来获取 div 标签之间的文本
<div class="col-lg-3 col-md-6 mb-4">
<div class="pricing-table pricing-secondary">
<div class="price-header">
<h3 class="title text-white">Gold Plan</h3>
<div class="price">
<span class="dollar">₹</span> 7000
</div>
<span class="permonth">3 months</span>
</div>
<div class="price-footer">
<a data-toggle="modal" data-target="#myModal"
class="btn btn-primary btn-block btn-lg" href="javascript:void(0);">Book an
Online Appointment</a>
</div>
</div>
</div>
<div class="col-lg-3 col-md-6 mb-4">
<div class="pricing-table pricing-third">
<div class="price-header">
<h3 class="title text-white">Diamond Plan</h3>
<div class="price text-primary">
<span class="dollar">₹</span> 12000
</div>
<span class="permonth">6 Months</span>
</div>
<div class="price-footer">
<a data-toggle="modal" data-target="#myModal"
class="btn btn-primary btn-block btn-lg" href="javascript:void(0);">Book an
Online Appointment</a>
</div>
</div>
</div>
<div class="col-lg-3 col-md-6 mb-4">
<div class="pricing-table pricing-fourth">
<div class="price-header">
<h3 class="title text-white">Platinum Plan</h3>
<div class="price text-white">
<span class="dollar">₹</span> 15000
</div>
<span class="permonth">12 Months</span>
</div>
<div class="price-footer">
<a data-toggle="modal" data-target="#myModal"
class="btn btn-primary btn-block btn-lg" href="javascript:void(0);">Book an
Online Appointment</a>
</div>
</div>
</div>
I have this type of divs its around three or four I want to detect the value 12000
and the month 6 Months
by clicking on this button我有这种类型的 div 大约三个或四个我想通过单击此按钮来检测值
12000
和月份6 Months
How can I achieve that.我怎样才能做到这一点。
Try尝试
$(".price-footer .btn").on( "click", function(event) {
const parent = $(this).parents('.pricing-table');
const priceText = $('.price', parent).text().trim();
const priceParts = priceText.split(' ');
console.log(`priceText: ${priceParts[1]}`);
// Output: "priceText: 12000"
const permonthText = $('.permonth', parent).text().trim();
console.log(`permonthText: ${permonthText}`);
// Output: "permonthText: 6 Months"
});
Demo - https://codepen.io/vyspiansky/pen/MWybEEr演示 - https://codepen.io/vyspiansky/pen/MWybEEr
You can add id
s to the elements you want to extract the contents of, then you can use document.getElementById(id)
to get those elements, and use .innerText
to extract the string contents.您可以将
id
添加到要提取其内容的元素中,然后可以使用document.getElementById(id)
获取这些元素,并使用.innerText
提取字符串内容。
Then with a string.replace()
you can remove any non-numerical values, and convert the monetary value into a number.然后使用
string.replace()
您可以删除任何非数字值,并将货币值转换为数字。
const dollarAmount = parseFloat(document.getElementById("dollar-info").innerText.replace(/[^0-9.]/g, '')) const month = document.getElementById("month-info").innerText console.log(dollarAmount) console.log(month)
<div class="price-header"> <h3 class="title text-white">Diamond Plan</h3> <div id="dollar-info" class="price text-primary"> <span class="dollar">₹</span>12000.80 </div> <span id="month-info" class="permonth">6 Months</span> </div>
Assuming you have many price tables you can loop each table as below example.假设您有许多价格表,您可以循环每个表,如下例所示。 You can wrap you data in a certain class and get the text value of that class div.
您可以将数据包装在某个 class 中,并获取该 class div 的文本值。 Click the button on the to get the value in the example below
单击上的按钮以获取以下示例中的值
$(document).ready(function(){ $('.pricing-table').each(function(){ var $table = $(this); $table.on('click', '.btn-primary', function(){ var price = $table.find('.price').text().trim(); var permonth = $table.find('.permonth').text().trim(); alert("price is " + price + " for " + permonth); }); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-lg-3 col-md-6 mb-4"> <div class="pricing-table pricing-third"> <div class="price-header"> <h3 class="title text-white">Diamond Plan</h3> <div class="price text-primary"> <span class="dollar">₹</span> 12000 </div> <span class="permonth">6 Months</span> </div> <div class="price-footer"> <a data-toggle="modal" data-target="#myModal" class="btn btn-primary btn-block btn-lg" href="javascript:void(0);">Book an Online Appointment</a> </div> </div> </div>
There are so many options to do this.有很多选择可以做到这一点。 Here is my example with the snippet and code sandbox.
这是我的代码片段和代码沙箱示例。
// jquery const findValues = (target, value) => target.parents().eq(1).find(value); $(document).on("click", ".btn", (e) => { const $target = $(e.target); // dollar is optional const $dollar = findValues($target, ".dollar"); const $value = findValues($target, ".value"); const $month = findValues($target, ".permonth"); $("#test").text(`${$dollar.text()} ${$value.text()} ${$month.text()}`); }); // // vanilla js const test1 = document.querySelector("#test1"); document.querySelectorAll(".btn").forEach((btn) => { btn.addEventListener("click", ({ target }) => { const nodes = target.parentNode.parentNode; const dollar1 = nodes.querySelector(".dollar"); const value1 = nodes.querySelector(".value"); const month1 = nodes.querySelector(".permonth"); test1.innerText = `${dollar1.textContent} ${value1.textContent} ${month1.textContent}`; }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-lg-3 col-md-6 mb-4"> <div class="pricing-table pricing-secondary"> <div class="price-header"> <h3 class="title text-white">Gold Plan</h3> <div class="price"> <span class="dollar">₹</span> <span class="value">7000</span> </div> <span class="permonth">3 months</span> </div> <div class="price-footer"> <a data-toggle="modal" data-target="#myModal" class="btn btn-primary btn-block btn-lg" href="javascript:void(0);">Book an Online Appointment</a> </div> </div> </div> <div class="col-lg-3 col-md-6 mb-4"> <div class="pricing-table pricing-third"> <div class="price-header"> <h3 class="title text-white">Diamond Plan</h3> <div class="price text-primary"> <span class="dollar">₹</span> <span class="value">12000</span> </div> <span class="permonth">6 Months</span> </div> <div class="price-footer"> <a data-toggle="modal" data-target="#myModal" class="btn btn-primary btn-block btn-lg" href="javascript:void(0);">Book an Online Appointment</a> </div> </div> </div> <div class="col-lg-3 col-md-6 mb-4"> <div class="pricing-table pricing-fourth"> <div class="price-header"> <h3 class="title text-white">Platinum Plan</h3> <div class="price text-white"> <span class="dollar">₹</span> <span class="value">15000</span> </div> <span class="permonth">12 Months</span> </div> <div class="price-footer"> <a data-toggle="modal" data-target="#myModal" class="btn btn-primary btn-block btn-lg" href="javascript:void(0);">Book an Online Appointment</a> </div> </div> </div> <div id="test"></div> <div id="test1"></div>
https://codesandbox.io/s/eager-voice-2w640?file=/src/index.js https://codesandbox.io/s/eager-voice-2w640?file=/src/index.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.