jquery sort out li
I have code
<ul id="list">
<li>test</li>
<li class="header">test</li>
<li>test</li>
</ul>
I need execute this code
(use jquery)
var items = $("#list li");
开发者_JS百科$.each(items, function (key, value) {
if ($(value).hasClass(".header")) {
... Do Something ...
}
}
this code doesn't work, because value == 'test'
Excuse, my English is awful
I don't know what you mean by value == 'test'
. There's nothing like that in your question.
Your $.each()
is missing its closing )
.
Also, your .hasClass
should not include the .
in the class name.
So this:
.hasClass(".header")
should be:
.hasClass("header")
Although, if you're interested in running code against only the items that have a class, just do a .filter()
before .each()
.
items.filter('.header').each( function (key, value) {
... Do Something ...
});
If you have no other use for the collection, then you could modify the selector like this:
$('#list li.header').each( function (key, value) {
... Do Something ...
});
But only if you have no other need to cache all of the <li>
elements.
You are using a . in your hasClass functon while passing class name. Change it to:
var items = $("#list li");
$.each(items, function (key, value){
if ($(value).hasClass("header")){
alert('Hi');
//Do Something
}
});
精彩评论