Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.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/jquery/75.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
Javascript 在jQuery条件语句中定位li元素的第二个子元素_Javascript_Jquery_Css - Fatal编程技术网

Javascript 在jQuery条件语句中定位li元素的第二个子元素

Javascript 在jQuery条件语句中定位li元素的第二个子元素,javascript,jquery,css,Javascript,Jquery,Css,我遇到了一个问题,我似乎无法将li元素的2nd子元素作为目标,并在条件语句中使用它。jQuery条件是否不适用于li:n子级(2) if($(“.steps ul li:first child”).attr('aria-selected')=“true”){ $(“.ul li:第一个孩子”)。附加(“”); } if(“.steps ul li:nth child(2)”).hasClass(“.current”)){ $(“.ul li:n子项(2)”)。附加(“”); } 您需要添加$选择

我遇到了一个问题,我似乎无法将
li
元素的
2nd
子元素作为目标,并在条件语句中使用它。jQuery条件是否不适用于
li:n子级(2)

if($(“.steps ul li:first child”).attr('aria-selected')=“true”){
$(“.ul li:第一个孩子”)。附加(“”);
}
if(“.steps ul li:nth child(2)”).hasClass(“.current”)){
$(“.ul li:n子项(2)”)。附加(“”);
}

您需要添加
$
选择器并从选择器中删除

if(".steps ul li:nth-child(2)").hasClass(".current"))
// ^ missing $ here                     //^--- Remove this dot
应该是

if($(".steps ul li:nth-child(2)").hasClass("current"))

您需要添加
$
选择器并从选择器中删除

if(".steps ul li:nth-child(2)").hasClass(".current"))
// ^ missing $ here                     //^--- Remove this dot
应该是

if($(".steps ul li:nth-child(2)").hasClass("current"))

哦,是的,对不起,我一定是把它复制粘贴错了。它似乎仍然不起作用。下面是第二个孩子的HTML代码
  • @RobertAllenBaker你能做一把小提琴吗?我将尝试找出问题所在。基本上,我要做的是用进度条的图像替换JQuery步骤的文本步骤。@RobertAllenBaker在这里,它工作正常,请检查它。@RobertAllenBaker根据您的JQuery选择器,只有当
    ul
    .steps
    中时,它才会起作用,但是
    HTML
    中没有这样的东西。将代码包装在一个
    div
    中,使用class
    steps
    将JavaScript代码包装在
    $(function(){…})中以便在代码运行时找到DOM元素。哦,是的,对不起,我一定是复制并粘贴错了。它似乎仍然不起作用。下面是第二个孩子的HTML代码
  • @RobertAllenBaker你能做一把小提琴吗?我将尝试找出问题所在。基本上,我要做的是用进度条的图像替换JQuery步骤的文本步骤。@RobertAllenBaker在这里,它工作正常,请检查它。@RobertAllenBaker根据您的JQuery选择器,只有当
    ul
    .steps
    中时,它才会起作用,但是
    HTML
    中没有这样的东西。将代码包装在一个
    div
    中,使用class
    steps
    将JavaScript代码包装在
    $(function(){…})中以便在代码运行时查找DOM元素。