首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >显示nextUntil参数被忽略

显示nextUntil参数被忽略
EN

Stack Overflow用户
提问于 2013-05-02 00:05:28
回答 1查看 409关注 0票数 1

在下面的代码中,我希望看到显示红色、蓝色、绿色和棕色行,但似乎除了红色之外的所有行都被隐藏,就像nextUntil参数被忽略一样。有什么线索吗?谢谢。

在一些研究之后,我尝试了这两种方法

代码语言:javascript
运行
复制
$("#firstrow").nextUntil('span[class="hues"]').hide();

代码语言:javascript
运行
复制
$("#firstrow").nextUntil('span:not(.colors)').hide();   

HTML:

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"/>
        <script>
            $(document).ready(function() {
                $("#firstrow").nextUntil('span[class="hues"]').hide();
                //$("#firstrow").nextUntil('span:not(.colors)').hide();            
            });
        </script>
    </head>
    <body>
        <table id="colortable">
            <tr id="firstrow">
                <span class="colors">Red</span>
            </tr>
            <tr>
                <span class="colors">Orange</span>
            </tr>
            <tr>
                <span class="hues">Blue</span>
            </tr>
            <tr>
                <span class="shades">Green</span>
            </tr>
            <tr>
                <span class="colors">Brown</span>
            </tr>
        </table>
    </body>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-02 00:17:50

您的标记无效,tr元素应该只有td子元素。

代码语言:javascript
运行
复制
<table id="colortable">
    <tbody>
        <tr id="firstrow">
            <td> 
                <span class="colors">Red</span>
            </td>
        </tr>
        ...
    </tbody>
</table>

跨度元素不是所选元素的同级元素,nextUntil仅筛选所选元素的下一个同级元素,您应该选择具有该特定跨度元素的tr

代码语言:javascript
运行
复制
// Select the all next sibling elements until the first element that matches the selector
$("#firstrow").nextUntil('tr:has(span.hues)').hide();

http://jsfiddle.net/KDKeF/

如果要选择与选择器匹配的所有下一个同级元素,可以使用nextAll方法:

代码语言:javascript
运行
复制
// Select the all next sibling elements that match the selector
$("#firstrow").nextAll('tr:has(span.hues)').hide();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16321811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档