[英]how to capture text in different variables with same class name in javascript jquery
I need to capture the texts ie'You're not sure about' and 'Ways to get assistance' in different variables. 我需要捕获不同变量中的文本,例如“您不确定”和“寻求帮助的方式”。 however when i try using this 但是当我尝试使用这个
$('.perind-details__summary-text').text();
I am getting result in same variable as " You're not sure about Ways to get assistance " How to capture these two different texts in different variables? 我在与“您不确定获得帮助的方式”相同的变量中得到的结果如何在不同的变量中捕获这两个不同的文本?
<details class="perind-details" open="">
<summary class="perind-details__summary" role="button" volu-controls="short-content-0" volu-expanded="true">
<span class="perind-details__summary-text">
You're not sure about
</span>
<details class="perind-details" open="">
<summary class="perind-details__summary" role="button" volu-controls="short-content-0" volu-expanded="true">
<span class="perind-details__summary-text">
Ways to get assistance
</span>
</summary>
Use .map
first instead: 首先使用.map
:
const texts = $('.perind-details__summary-text') .map(function() { return $(this).text().trim() }) .get(); console.log(texts); const [text1, text2] = texts;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <details class="perind-details" open=""> <summary class="perind-details__summary" role="button" volu-controls="short-content-0" volu-expanded="true"> <span class="perind-details__summary-text"> You're not sure about </span> <details class="perind-details" open=""> <summary class="perind-details__summary" role="button" volu-controls="short-content-0" volu-expanded="true"> <span class="perind-details__summary-text"> Ways to get assistance </span> </summary>
Or, no need for a big library like jQuery for something this simple: 或者,不需要像jQuery这样的大型库就可以实现以下简单功能:
const texts = Array.prototype.map.call( document.querySelectorAll('.perind-details__summary-text'), elm => elm.textContent.trim() ); console.log(texts);
<summary class="perind-details__summary" role="button" volu-controls="short-content-0" volu-expanded="true"> <span class="perind-details__summary-text"> You're not sure about </span> <details class="perind-details" open=""> <summary class="perind-details__summary" role="button" volu-controls="short-content-0" volu-expanded="true"> <span class="perind-details__summary-text"> Ways to get assistance </span> </summary>
You should use id attribute to make it easier for the selector to find it. 您应该使用id属性使选择器更容易找到它。
<details class="perind-details" open="">
<summary class="perind-details__summary" role="button" volu-controls="short-content-0" volu-expanded="true">
<span id="perind-details__summary-text-1">
You're not sure about
</span>
<details class="perind-details" open="">
<summary class="perind-details__summary" role="button" volu-controls="short-content-0" volu-expanded="true">
<span id="perind-details__summary-text-2">
Ways to get assistance
</span>
</summary>
then you use it like this to select the specific id. 然后像这样使用它来选择特定的ID。
var text1 = $('#perind-details__summary-text-1').text();
var text2 = $('#perind-details__summary-text-2').text();
I assume you're using class in CSS. 我假设您在CSS中使用类。 So you can't just change class to solve the issue, but why don't you add an ID to each input? 因此,您不能仅更改类来解决问题,但为什么不为每个输入添加一个ID?
var text1 = $('.perind-details__summary-text').eq(0).text();
var text2 = $('.perind-details__summary-text').eq(1).text();
A better way if you don't want to use ID. 如果您不想使用ID,则是更好的方法。 Then create a variable as an array. 然后创建一个变量作为数组。 Iterate the class node then you can push that text in the array. 迭代类节点,然后您可以将该文本推送到数组中。 Later you can use that array to use pushed text wherever you want. 以后,您可以使用该数组在任何需要的地方使用推入文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.