Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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 查找当前类是否包含word,然后将新类附加到这些li';s-jquery_Javascript_Jquery_Html Lists_Each - Fatal编程技术网

Javascript 查找当前类是否包含word,然后将新类附加到这些li';s-jquery

Javascript 查找当前类是否包含word,然后将新类附加到这些li';s-jquery,javascript,jquery,html-lists,each,Javascript,Jquery,Html Lists,Each,jQuery方法有一些问题-可能是太复杂了 我需要找到的是,如果任何li元素类包含单词“current”,那么如果它们包含单词“current”,则将单词“active”附加到它们后面 我正在努力将这个词添加到当前课程的末尾。例如: 标记: <div class="menu-navigation-container"> <ul> <li class="current_page_item menu-item-8787"><a hre

jQuery方法有一些问题-可能是太复杂了

我需要找到的是,如果任何li元素类包含单词“current”,那么如果它们包含单词“current”,则将单词“active”附加到它们后面

我正在努力将这个词添加到当前课程的末尾。例如:

标记:

<div class="menu-navigation-container">
    <ul>
        <li class="current_page_item menu-item-8787"><a href="http://www.cornermag.com/mag/" style="font-size: 1.6rem;"><span>The Magazine</span></a>
        </li>
           <li class="menu-item menu-item-type-post_type"><a href="http://www.cornermag.com/snapped/" style="font-size: 1.6rem;"><span>Snapped</span></a>
           </li>
    </ul>
</div>
$(document).ready(function () {
    var classNames = $('.menu-navigation-container ul li').attr("class").match(/[\w-]*current[\w-]*/g);
    $(classNames).each(function () {
        $(this).addClass("active");
    });
});
运行
类名up只生成字符串-我希望它引用类名中有单词'current'的li Elemen,然后在末尾附加单词'active'

我可以用jQuery的吗

就像这样:

$(document).ready(function () {
    $('.menu-navigation-container ul li[class*=current]').addClass('active');
});
就这样:

$(document).ready(function () {
    $('.menu-navigation-container ul li[class*=current]').addClass('active');
});
就这样:

$(document).ready(function () {
    $('.menu-navigation-container ul li[class*=current]').addClass('active');
});
就这样:

$(document).ready(function () {
    $('.menu-navigation-container ul li[class*=current]').addClass('active');
});
试试这个

 $(document).ready(function () {


    $('.menu-navigation-container ul li').each(function(){

     if ( $(this).is(".current") )
      {
           $(this).addClass("active");
      }

    })




    });
试试这个

 $(document).ready(function () {


    $('.menu-navigation-container ul li').each(function(){

     if ( $(this).is(".current") )
      {
           $(this).addClass("active");
      }

    })




    });
试试这个

 $(document).ready(function () {


    $('.menu-navigation-container ul li').each(function(){

     if ( $(this).is(".current") )
      {
           $(this).addClass("active");
      }

    })




    });
试试这个

 $(document).ready(function () {


    $('.menu-navigation-container ul li').each(function(){

     if ( $(this).is(".current") )
      {
           $(this).addClass("active");
      }

    })




    });


用选择器直接进入有什么问题?e、 g.
.menu导航容器ul li[class*=“current”]
为什么不作为
current\u page\u item
检查特定类?您的匹配项将查找名称中包含
current
的所有字符串。为什么现在不重新检查相同的元素,如果它们具有给定的类,则添加active。通过
将单词active附加到它们
您的意思是在类列表中添加
active
,还是将类名重命名为
current***active
直接使用选择器有什么不对?e、 g.
.menu导航容器ul li[class*=“current”]
为什么不作为
current\u page\u item
检查特定类?您的匹配项将查找名称中包含
current
的所有字符串。为什么现在不重新检查相同的元素,如果它们具有给定的类,则添加active。通过
将单词active附加到它们
您的意思是在类列表中添加
active
,还是将类名重命名为
current***active
直接使用选择器有什么不对?e、 g.
.menu导航容器ul li[class*=“current”]
为什么不作为
current\u page\u item
检查特定类?您的匹配项将查找名称中包含
current
的所有字符串。为什么现在不重新检查相同的元素,如果它们具有给定的类,则添加active。通过
将单词active附加到它们
您的意思是在类列表中添加
active
,还是将类名重命名为
current***active
直接使用选择器有什么不对?e、 g.
.menu导航容器ul li[class*=“current”]
为什么不作为
current\u page\u item
检查特定类?您的匹配项将查找名称中包含
current
的所有字符串。为什么现在不重新检查相同的元素,如果它们具有给定的类,则添加active。通过
将单词active附加到它们
您的意思是在类列表中添加
active
,还是将类名重命名为
current***active
不一定与OP使用的regexp相同。不过,它可能仍然可以,这取决于OP是否希望查找
blackcurrent类
,它仍将匹配。*表示类在某处包含该单词。不一定与OP使用的regexp相同。不过,它可能仍然可以,这取决于OP是否希望查找
blackcurrent类
,它仍将匹配。*表示类在某处包含该单词。不一定与OP使用的regexp相同。不过,它可能仍然可以,这取决于OP是否希望查找
blackcurrent类
,它仍将匹配。*表示类在某处包含该单词。不一定与OP使用的regexp相同。不过,它可能仍然可以,这取决于OP是否希望查找
blackcurrent类
,它仍将匹配。*意思是说这个类在某处包含那个词。