开发者

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>


  1. #server can only match a single element.
  2. counter is undefined.
  3. #list does not contain any items, so #list li will not match anything.
  4. You are missing a period before append.


I see two problems.

  1. $('#list li') doesn't resolve to anything since there is no <li> yet. It should be ul instead.
  2. 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 maybe counter 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>');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜