简体   繁体   English

如何在javascript jquery中使用相同的类名捕获不同变量中的文本

[英]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.

相关问题 如何使用javascript或jquery从具有相同CSS类名称的两个不同下拉列表中选择选项 - How to select options from two different dropdowns of same css class name using javascript or jquery 如何在javascript中重新声明具有相同名称但属于不同文件的变量? - How to redeclare variables with the same name but belonging to different files in javascript? 具有相同类名的不同元素的相同jquery - Same jquery for different elements with same class name jquery如何触发具有相同类但不同文本的链接的点击 - jquery How to trigger click of links with same class but different text javascript:对于不同变量的同一类的不同对象复制数组 - javascript: for different objects of same class copy arrays in different variables 如何在addEventListener中捕获两个相同的ID但不同的类 - How to capture two same id but different class in addEventListener Javascript在具有相同类但不同变量的多个div中运行函数 - Javascript run function inside multiple divs with the same class but different variables 如何在两个不同的事件上的同一个HTML控件(文本字段)上调用两个不同的javascript / jquery函数? - How to call two different javascript/jquery functions on same HTML control(text field) on two different events? 如何在相同的CLASS名称上添加不同的ID? - How to add different ID on same CLASS name? 如何使用JavaScript或jQuery将纯文本网站捕获为变量? - How to capture plain text site into a variable using JavaScript or jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM