admin管理员组文章数量:1320888
I did a bit of Googling, and I found that the following code does not work because inputs[i].className is not a string. How would I make this a string??? I tried toString(). Also, inputs.length is not 0. I checked.
for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].className.indexOf("blah") == 1)
{
//do something
}
}
I want to do something with only the inputs array values that have a class name of something like "blah 1;2;3;4".
Any help would be appreciated.
I did a bit of Googling, and I found that the following code does not work because inputs[i].className is not a string. How would I make this a string??? I tried toString(). Also, inputs.length is not 0. I checked.
for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].className.indexOf("blah") == 1)
{
//do something
}
}
I want to do something with only the inputs array values that have a class name of something like "blah 1;2;3;4".
Any help would be appreciated.
Share Improve this question edited Nov 17, 2012 at 6:54 vivek salve 9911 gold badge10 silver badges20 bronze badges asked Nov 17, 2012 at 6:39 user963070user963070 6392 gold badges19 silver badges24 bronze badges 2-
The className doesn't have an
indexOf
method. – ichigolas Commented Nov 17, 2012 at 6:46 -
1
@nicooga
(typeof document.createElement('input').className) === 'string'
; – xiaoyi Commented Nov 17, 2012 at 6:48
2 Answers
Reset to default 5indexOf(value)
returns -1
when no match, and a index starting with 0
when there is match.
In your case, you should pare the result with 0, instead of 1.
Note that, if you want to test if a node has some certain class name, this is not a good practice.
A better approach is to use DOM API node.classList.contains()
which is available in modern browsers.
Or use regular expression /\bblah\b/.test(node.className)
to avoid the cases that blah
is a substring of another class name, say not-blah
.
Another way is to use mootools, which provides node.hasClass()
to HTMLElement
instances.
Or use jQuery like $(node).hasClass()
.
If you want to check if a class is present, check that indexOf() !== -1 as indexOf returns the index at which the searched for term begins. Checking for 1 would only work if it began at the second character. Similarly, checking for 0 will only work if it begins at the start, but if there is more than one class, it could appear at any number of other indices, in which case it would return some value other than 0.
As xiaoyi points out, you could have class names that are similar enough to the class name you're looking for that it could return false positives. To avoid that, if you still wanted to use indexOf, you would need to do some additional checking. First, I would split the className on ' ' and iterate through the resulting array doing the index check. Then, on any indexOf() !== -1 values, check that the length of the value is equal to the length of the value you're checking against.
本文标签: javascriptstuffclassNameindexOf(quottermquot)1 not workingStack Overflow
版权声明:本文标题:javascript - stuff.className.indexOf("term") == 1 not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741999339a2410689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论