简体   繁体   English

获取元素数组中的每个元素值

[英]Get each element value inside elements array

I need to find elements value inside elements array. 我需要在elements数组中找到elements值。 Note, that there are many <a> elements, but only one <span id="attendees"> inside one <a> . 注意,有许多<a>元件,但只有一个<span id="attendees">一个内部<a>

As following example: 如下例:

HTML: HTML:

<a id="meetingItem_{{meeting.id}}" onclick="AuthorityCheck()"> Item

      <span id="attendees" style="visibility:collapse;" >{{meeting.attendees}}</span>
</a>

Javascript: Javascript:

function AuthorityCheck(){

          var items = $('*[id^="meetingItem_"]');
          for (var i = 0; i < items.length; i++){
               var people = items[i].id.indexOf("attendees"); // I need to find span value here, but get undefined.
          }
    }

It can be with jQuery as well, but I would prefer Javascript. jQuery也可以,但我更喜欢Javascript。 :) :)

function AuthorityCheck()
{
      var items = document.querySelectorAll('*[id^="meetingItem_"]');
      for (var i = 0; i < items.length; i++)
      {
           var people = document.getElementById("attendees").innerHTML;  
      }
}

This solution is fully vanilly and uses no jQuery. 此解决方案完全无效,不使用jQuery。 innerHTML is uses because it gives the content back in every browser. 使用innerHTML是因为它在每个浏览器中都提供了内容。 If there is HTML that shouldn't be retrieved. 是否存在不应检索的HTML。 you should use innerText or textContent. 您应该使用innerText或textContent。 Change the line to this: 将行更改为此:

 var people = document.getElementById("attendees").textContent || document.getElementById("attendees").innerText; 

On a side note: Id is unique for the complete DOM tree . 附带说明: Id对于完整的DOM tree是唯一的。 That means if you have multiple a elements with attendees span in it. 这意味着如果您有多个包含参与者span a元素。 The code will not work properly since there are multiple spans with id = attendees . 该代码将无法正常运行,因为存在多个spansid = attendees spans You can circumnavigate this by using attributes or classes. 您可以使用属性或类对此进行绕行。

 <span data-type="attendees" style="visibility:collapse;" >{{meeting.attendees}}</span>
function AuthorityCheck()
{
      var items = document.querySelectorAll('*[id^="meetingItem_"]');
      for (var i = 0; i < items.length; i++)
      {
           var people = items[i].querySelectorAll("span[data-type='attendees']")[0].innerHTML;  
      }
}

Have you tried: 你有没有尝试过:

var people = document.getElementById("attendees").textContent;

?

Or the cross-browser-friendly: 或跨浏览器友好:

var people = $("#attendees").text();

If the the item has the 'attendess' id just take the value of that element, if your element is an span you can store your value in a data-attribute : 如果项目具有“出席者” ID,则只需获取该元素的值,如果您的元素是跨度,则可以将值存储在data-attribute

JS JS

 function AuthorityCheck(){
      var items = $('*[id^="meetingItem_"]');
        for (var i = 0; i < items.length; i++){
          if (items[i].id.indexOf("attendees") !== -1) {
            var people = items[i].getAttribute('data-value');
          }
        }
    }

HTML 的HTML

<span id="attendees" data-value="your value">

how about this 这个怎么样

var people = [];
$('*[id^="meetingItem_"]').each(function (index) {
    people[index] = $(this).find('span#attendees').html();
});

暂无
暂无

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

相关问题 从每个数组元素中获取一个属性值 - Get a property value from each array element 数组总是在每个元素中获取最后一个值 - Array always get last value in each element 使用Lodash获取数组中每个数组的第一个元素 - Use Lodash to get first element of each array inside array HandlebarsJS:获取每个数组的元素i,然后获取元素j等? - HandlebarsJS : Get elements i of each array, then element j, etc.? 删除每个元素后如何获取数组元素的所有组合 - How to get all combinations of array elements after removing each element 隔离嵌套数组中的数组元素,并将其用作JSON字段值,并将其同级数组元素用作字段值 - Isolate an array element inside nested array and use it as the JSON field value with it's sibling array elements as a field value JavaScript:将偶数元素的和添加到给定数组中的每个奇数值元素; 显示新数组 - JavaScript: Add sum of even value elements to each odd value element in an given array; show the new array 赛普拉斯在每个内部获取元素 - Cypress get element inside each 如何从元素数组中动态获取元素的值? - How do I dynamically get the value of an element from an array of elements? 如何以不同的超时延迟显示数组的每个元素,每个延迟时间是反应中每个元素内的值 - How to display each element of an array with different timeout delay and each delay time being a value inside each element in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM