Jquery nearest()和match()将不起作用

时间:2017-08-28 14:30:54

标签: jquery html

我为此设置了一个jsfiddle:http://jsfiddle.net/zatHH/422/

如果<a>包含“找不到短语”,我试图隐藏父div。

可悲的是,我在那里写的东西不起作用,如果有人能帮助我,我会很感激。

谢谢!

2 个答案:

答案 0 :(得分:0)

您错误地使用match因为它需要正则表达式,而且您正试图在jQuery对象上使用它。无论如何,jQuery有:contains这将满足您的需求:

 $('#cd_category_0 li a:contains("No phrase found by")').closest('.cd-extendable').hide();

&#13;
&#13;
$('#cd_category_0 li a:contains("No phrase found by")').closest('.cd-extendable').hide();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div class="cd-extendable opened">
    <div class="dropdown">All Categories</div>
    <div class="box">
        <div class="bc" style="display: none;"></div>
        <div class="uls">
            <ul id="cd_category_0">
                <li accesskey="" class="selected"><a href="javascript://">All Categories</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
                <li accesskey=""><span title="Show Subcategories"></span><a href="javascript://">No phrase found by
                    "pages+name+lt_" key</a></li>
            </ul>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果使用:contains(),它应该为您提供所需的输出。

    $("#cd_category_0 li a:contains('No phrase found by')").closest('.cd-extendable').hide();