[英]Select element by element's content
I have list: 我有清单:
<ul class='mates'>
<li class='m' id='1'>Jakub</li>
<li class='f' id='2'>Vinnie</li>
<li class='m' id='3'>David</li>
</ul>
How can I select 'li' tags "ONE BY ONE" to be checked if their content (between 'li' tags) is equal to 'xyz'. 如果“ li”标签的内容(“ li”标签之间)等于“ xyz”,如何选择“ lione”标签“ ONE BY ONE”。
element = document.getElementsByClassName('.mates').firstChield.innerHTML;
do {
if(){
//do something
}
}while (element = element.nextSibling);
but I'm not getting even far enough to select firstChild. 但我还远远不能选择firstChild。 This error is showing in console: "Cannot read property 'innerHTML' of undefined".
控制台中显示此错误:“无法读取未定义的属性'innerHTML'”。 This needs to be done in plain JavaScript.
这需要用普通的JavaScript完成。 Any ideas?
有任何想法吗?
Drop the dot in the parameter. 将点放在参数中。 Like this:
像这样:
element = document.getElementsByClassName('mates').firstChild.innerHTML;
The dot is not a part of the name of the class. 点不是类名称的一部分。
EDIT also notice that the question originally had a typo in firstChild
. 编辑还注意到该问题最初在
firstChild
有一个错字。
Your element
variable is not an element (its value is probably undefined
). 您的
element
变量不是元素(其值可能是undefined
)。 It should work if you use it like this: 如果您这样使用它,它应该可以工作:
var element = document.getElementsByClassName('mates')[0].firstChild;
do {
if(element.innerHTML == 'foo'){
//do something
}
} while (element = element.nextSibling);
The code above fixes: 上面的代码修复了:
.firstChild
.firstChild
的错字 Also note that getElementsByClassName
returns a list of elements, so you have to grab the first one in the list (index 0
) to reach your <ul>
. 还要注意,
getElementsByClassName
返回一个元素列表,因此您必须获取列表中的第一个元素(索引0
)才能到达<ul>
。
Finally, keep in mind that you'll be looping over all children of the <ul>
, including empty text nodes (see a demonstration at http://jsfiddle.net/58ZZF/ ). 最后,请记住,您将遍历
<ul>
所有子级,包括空文本节点(请参见http://jsfiddle.net/58ZZF/上的演示)。 This can be avoided if you use firstElementChild
and nextElementSibling
, but I'm not sure if there are cross browsers issues with those properties (MDN only says it's Firefox 3.5+). 如果您使用
firstElementChild
和nextElementSibling
可以避免这种情况,但是我不确定这些属性是否存在跨浏览器问题(MDN仅表示它是Firefox 3.5+)。
Few mistakes 少有错误
getElementsByClassName
should not have .
getElementsByClassName
类名称不应具有.
firstChild
firstChild
拼写错误 getElementsByClassName
returns an array, not a dom reference getElementsByClassName
返回一个数组,而不是dom引用 nextSibling
it could return text nodes also, you need to check the nodeType to make sure the element is a element node(nodeTye = 1), also you can check the tagName == 'LI'
nextSibling
它也可能返回文本节点,您需要检查nodeType以确保元素是元素节点(nodeTye = 1),还可以检查tagName == 'LI'
Try 尝试
var element = document.getElementsByClassName('mates')[0].firstChild;
do {
if(element.nodeType == 1){
console.log(element.textContent)
}
}while (element = element.nextSibling);
<ul class='mates'>
<li class='m' id='1'>Jakub</li>
<li class='f' id='2'>Vinnie</li>
<li class='m' id='3'>David</li>
</ul>
<script>
var mates = document.getElementsByClassName('mates')[0];
for (var i=0; i< mates.childNodes.length; i++){
if(mates.children[i].innerHTML == 'Vinnie') alert("Got you! ID "+mates.children[i].id)
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.