最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: missing : after property id - Stack Overflow

programmeradmin1浏览0评论
$('.pagination ul li').each({ 
if( $(this).index(this) > 2  )
{
   $(this).hide();
}
 });

SyntaxError: missing : after property id whats the problem?

$('.pagination ul li').each({ 
if( $(this).index(this) > 2  )
{
   $(this).hide();
}
 });

SyntaxError: missing : after property id whats the problem?

Share Improve this question asked Sep 5, 2012 at 15:48 DenasDenas 411 gold badge1 silver badge3 bronze badges 4
  • 2 I'm not sure $(this).index(this) will get what you want. – gen_Eric Commented Sep 5, 2012 at 15:50
  • What version of jquery are you running? There was a bug related to this syntax in 1.8.0. It was fixed in 1.8.1 – gabaum10 Commented Sep 5, 2012 at 15:58
  • @gabaum10: What bug are you referring to? – gen_Eric Commented Sep 5, 2012 at 15:59
  • Oh wait, misread the question. Thought it was referring to a bug I saw with the new selector engine. – gabaum10 Commented Sep 5, 2012 at 16:03
Add a comment  | 

2 Answers 2

Reset to default 12

Use the keyword function preceding {} or it will be interpreted as an object literal.

$('.pagination ul li').each(function() { 
    if ($(this).index(this) > 2) {
        $(this).hide();
    }
});

Also, $(this).index(this) does not do what you expect. Did you want to check if the index at which the element is located is greater than 2? Use this revision instead:

$('.pagination ul li').each(function(idx) { 
    if (idx > 2) {
        $(this).hide();
    }
});

You need to pass .each a function. Without the function(), it's being read as an object ({}).

$('.pagination ul li').each(function(){ 
    if($(this).index(this) > 2){
        $(this).hide();
    }
});

P.S. $(this).index(this) does not do what you think it does. It will search inside this for this, therefore it always returns 0.

If you want the index of the li in the ul, use the index parameter from the .each.

$('.pagination ul li').each(function(index){ 
    if(index > 2){
        $(this).hide();
    }
});

P.P.S. If all you want to do is hide the lis that have an index > 2, then you can do this even easier:

$('.pagination ul li:gt(2)').hide();
发布评论

评论列表(0)

  1. 暂无评论