The eq fliter is not working
Why this code is not working? Meaning nothing get displayed 开发者_开发知识库into the ul tag.
$("#server").each(function()
{
var textValue = $(this).text();
$('#list li').eq(counter).append('<li>' + textValue + '</li>'); // <-- not working
$('#list li').eq(counter).insertAfter('<li>' + textValue + '</li>'); // <-- or not working
$('#list')append('<li>' + textValue + '</li>'); // <-- or working
});
<div>
<ul id="list"></ul>
</div>
#server
can only match a single element.counter
is undefined.#list
does not contain any items, so#list li
will not match anything.- You are missing a period before
append
.
I see two problems.
$('#list li')
doesn't resolve to anything since there is no<li>
yet. It should beul
instead.- If you're looking at a specific index with
eq
during iteration, you may want to pass in the current index into the functor, like so:$('#server').each(function(counter){...
- but maybecounter
comes from somewhere else, that's not clear from your posting.
Regards db
I think your full answer is here in your previous question on this topic (read the comments to your questions in my answer).
I think you want jQuery's method .after()
instead of .insertAfter()
. Full working example including a jsFiddle here.
$('#list li').eq(counter).after('<li>' + textValue + '</li>');
精彩评论