Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery css find first child给出错误的结果_Jquery_Css_Css Selectors - Fatal编程技术网

Jquery css find first child给出错误的结果

Jquery css find first child给出错误的结果,jquery,css,css-selectors,Jquery,Css,Css Selectors,我有: 此代码ul>#test1:第一个孩子很好,但ul>#test4:第一个孩子不工作 问题出在哪里?首先,元素的ID必须是唯一的 Second将只匹配第一个子元素,而不考虑用于该子元素的任何其他选择器,因此在您的情况下,第一个#test1将是与ul>:first child选择器匹配的唯一元素 我不认为有一个css唯一的解决方案 使用class属性代替ID,然后在jQuery中使用 ul > #test4:first-child { background:#ff0000; } 然后

我有:

此代码ul>#test1:第一个孩子很好,但ul>#test4:第一个孩子不工作


问题出在哪里?

首先,元素的ID必须是唯一的

Second将只匹配第一个子元素,而不考虑用于该子元素的任何其他选择器,因此在您的情况下,第一个#test1将是与
ul>:first child
选择器匹配的唯一元素

我不认为有一个css唯一的解决方案

使用class属性代替ID,然后在jQuery中使用

ul > #test4:first-child
{
background:#ff0000;
}
然后


它不起作用,因为您正在查找
元素的
:第一个子元素,该元素的id名为
test4
。您需要的是id为test4的第一个元素,我认为这在纯CSS中是不可能的


此外,您应该更好地使用类而不是ID,因为您不应该在HTML元素中重复ID。

我支持这一点[我本来打算自己建议,然后您编辑:)]
ul > #test4:first-child
{
background:#ff0000;
}
.someclass{
    //some rule
}
$('ul > .test4:first').addClass('someclass')