Javascript 语法错误:丢失:在属性 id 之后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12285213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: missing : after property id
提问by Denas
$('.pagination ul li').each({
if( $(this).index(this) > 2 )
{
$(this).hide();
}
});
SyntaxError: missing : after property id whats the problem?
SyntaxError: missing : 在属性 id 之后是什么问题?
回答by 0x499602D2
Use the keyword functionpreceding {}or it will be interpreted as an object literal.
使用function前面的关键字{},否则它将被解释为对象字面量。
$('.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:
此外,$(this).index(this)不会做你所期望的。是否要检查元素所在的索引是否大于 2?请改用此版本:
$('.pagination ul li').each(function(idx) {
if (idx > 2) {
$(this).hide();
}
});
回答by Rocket Hazmat
You need to pass .eacha function. Without the function(), it's being read as an object ({}).
您需要通过.each一个function. 如果没有function(),它将被作为对象 ( {})读取。
$('.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 thisfor this, therefore it always returns 0.
PS$(this).index(this)不会做你认为它会做的事情。它会搜索里面this的this,所以它总是返回0。
If you want the index of the liin the ul, use the index parameter from the .each.
如果你想要的索引li中ul,使用来自索引参数.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:
PPS 如果您只想隐藏li带有的s index > 2,那么您可以更轻松地做到这一点:
$('.pagination ul li:gt(2)').hide();

