【问题标题】:Hide elements with jquery using selector使用选择器隐藏带有jquery的元素
【发布时间】:2013-03-18 05:48:06
【问题描述】:

1 中的 6 个问题。假设我有这个:

<body>
    <form>
        <h3>blah</h3>
        <table>
            <tr>
                <th>Input 1:</th>
                <td><input type="text" id="some-input-a" name="some-input-a" /></td>
            </tr>
            <tr>
                <th>Input 2:</th>
                <td><input type="text" id="other-input" name="other-input" /></td>
            </tr>
        </table>
        <h3>blah</h3>
        <table>
            <tr>
                <th>Input 3:</th>
                <td>
                    <select name="my-select" id="my-select">
                        <option value="1">My 1</option>
                        <option value="c">Your f</option>
                        <option value="m">This g</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th>Input 4:</th>
                <td><input type="password" id="pass-input" name="next-input" /></td>
            </tr>
            <tr>
                <th>Input 5:</th>
                <td><input type="password" id="pass-confirm" name="pass-confirm" /></td>
            </tr>
        </table>
        <h3>blah</h3>
        <table>
            <tr>
                <th>Input 6:</th>
                <td><input type="text" id="next-one" name="next-one" /></td>
            </tr>
            <tr>
                <th>Input 7:</th>
                <td><input type="text" id="more-inputs" name="more-inputs" /></td>
            </tr>
            <tr>
                <th>Input 8:</th>
                <td><input type="text" id="form-input" name="form-input" /></td>
            </tr>
            <tr>
                <th>Input 9:</th>
                <td><textarea name="description" id="description" rows="5" cols="30"></textarea></td>
            </tr>
            <tr>
                <th>Input 10:</th>
                <td><input type="text" id="input-10" name="input-10" /></td>
            </tr>
        </table>
    </form>
</body>

有没有简单的隐藏方法:

  1. 第三张表完全
  2. Textarea所在的行
  3. 元素ID为“description”的行
  4. 第三张表的第二行
  5. 第三个&lt;h3&gt;
  6. 第五个输入标签

使用选择器

【问题讨论】:

  • 是的,您可以阅读 jQuery 文档,您肯定会找到解决方案 api.jquery.com/category/selectors
  • 花点时间阅读一下 jquery 选择器怎么样?
  • 很抱歉问这个问题,我自己尝试了一些方法来做到这一点,但我无法让它们发挥作用。我以我理解的方式使用选择器,但我的知识有限。无论如何,对不起,我会尽量不要再这样做了。

标签: jquery jquery-selectors hide


【解决方案1】:

1.

$('form table:eq(2)').hide();

2.

$('form table tr').has('textarea').hide();

3.

$('#description').hide();

4.

$('form table:eq(2) tr:eq(1)').hide();

5.

$('form h3:eq(2)').hide();

6.

$('form input:eq(4)').parent().prev('td').hide();

了解jQuery selectors

【讨论】:

  • 感谢您的帮助,我确实尝试过: $( "form > table:eq( 2 )" ).hide();但这对我不起作用。这就是我开始问的主要原因。这有什么理由不工作吗?
  • 应该可以。看起来选择器是对的。如果您看到任何错误,请检查浏览器。它在小提琴中工作
  • 感谢您的帮助。我最初的尝试是: $( "form#profile > table:eq( 3 )" ).hide(); ,出于某种原因,> 符号似乎是一个问题,我只是从其他地方复制了代码,现在我发现它有所不同。
【解决方案2】:

你使用 .hide() 来隐藏元素。你还有很多重复的问题。试试这个

 1.) $('table:eq(2)').hide();

    2.) $('table tr').has('textarea').hide();

    3.) $('#description').parent().parent().hide();

    4.) $('table:eq(2) tr:eq(1)').hide();    

    5.) $('h3:eq(2)').hide();

    6.) $('table input:ep(4)').hide();

【讨论】:

  • 感谢您的帮助。我会给你答案检查,因为你所有的建议都对我有用,但代码悖论首先回答了。
猜你喜欢
  • 2014-03-08
  • 2015-01-15
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多