[英]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
. 该代码将无法正常运行,因为存在多个
spans
为id
= 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.