简体   繁体   English

jQuery show / hide仅适用于第一个选择器

[英]jQuery show/hide only works on first selector

I'm trying to make a show hide function for my table. 我正在尝试为我的桌子做一个秀隐藏功能。 The problem is that the jQuery only seems to work for the first selector and no others. 问题是jQuery似乎只适用于第一个选择器,而没有其他选择器。

Can anyone help me with this problem? 谁能帮助我解决这个问题?

jQuery : jQuery的:

jQuery("document").ready(function($) {
    $("#paragraph-div").hide();

    $("#toggle-div").click(function() {
        if ($("#paragraph-div").is(":visible")) {
           $("#paragraph-div").hide();
            $("#toggle-div").val("Less Info");
        } else {
            $("#paragraph-div").show();
        }
    });
});

HTML : HTML:

<table>
    <tr>
        <td>data</td>
    </tr>
    <tr>
        <td>
            <a id="toggle-div">More Info</a>
            <div id="paragraph-div">
                <p>Hidden text - Toggle more info to show.</p>
            </div>
        </td>
    </tr>
    <tr>
        <td>data</td>
    </tr>
    <tr>
        <td>
            <a id="toggle-div">More Info</a>
            <div id="paragraph-div">
                <p>Hidden text - Toggle more info to show.</p>
            </div>
        </td>
    </tr>
    <tr>
        <td>data</td>
    </tr>
    <tr>
        <td>
            <a id="toggle-div">More Info</a>
            <div id="paragraph-div">
                <p>Hidden text - Toggle more info to show.</p>
            </div>
        </td>
    </tr>
</table>

Working fiddle 工作提琴

id should be unique in same document, replace the duplicate once by general class, eg : id在同一文档中应该是唯一的,请用通用类替换重复的副本,例如:

<a class="toggle-div">
<div class="paragraph-div">

Instead of : 代替 :

<a id="toggle-div">
<div id="paragraph-div">

Then use class selector . 然后使用类选择器. in your js instead of id selector : 在您的js中而不是id选择器中:

$(".toggle-div")
$(".paragraph-div")

Instead of : 代替 :

$("#toggle-div")
$("#paragraph-div")

NOTES : 注意事项:

  • You could use $(this) inside event instead of ".toggle-div" 您可以在事件中使用$(this)代替".toggle-div"
  • You should use $(this).next(".paragraph-div") to select the related paragraph with clicked link. 您应该使用$(this).next(".paragraph-div")选择带有单击链接的相关段落。
  • The div tag haven't a val() method you should use text() instead. div标签没有val()方法,应改用text()

Hope this helps. 希望这可以帮助。


Snippet 片段

 jQuery("document").ready(function($) { $(".paragraph-div").hide(); $(".toggle-div").click(function() { if ($(this).next(".paragraph-div").is(":visible")) { $(this).next(".paragraph-div").hide(); $(this).text("More Info"); } else { $(this).next(".paragraph-div").show(); $(this).text("Less Info"); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>data</td> </tr> <tr> <td><a class="toggle-div">More Info</a><div class="paragraph-div"><p>Hidden text - Toggle more info to show.</p></div></td> </tr> <tr> <td>data</td> </tr> <tr> <td><a class="toggle-div">More Info</a><div class="paragraph-div"><p>Hidden text - Toggle more info to show.</p></div></td> </tr> <tr> <td>data</td> </tr> <tr> <td><a class="toggle-div">More Info</a><div class="paragraph-div"><p>Hidden text - Toggle more info to show.</p></div></td> </tr> </table> 

I think you're looking for something like this : 我认为您正在寻找这样的东西:

 jQuery("document").ready(function($) { $(".paragraph-div").hide(); $(".toggle-div").click(function() { var $this = $(this); var $paragraph = $this.next('.paragraph-div'); if ($paragraph.is(":visible")) { $paragraph.hide(); $this.html("More Info"); } else { $paragraph.show(); $this.html("Less Info"); } }); }); 
 <script src="https://code.jquery.com/jquery-1.12.2.min.js"></script> <table> <tr> <td>data</td> </tr> <tr> <td> <a class="toggle-div">More Info</a> <div class="paragraph-div"> <p>Hidden text - Toggle more info to show.</p> </div> </td> </tr> <tr> <td>data</td> </tr> <tr> <td> <a class="toggle-div">More Info</a> <div class="paragraph-div"> <p>Hidden text - Toggle more info to show.</p> </div> </td> </tr> <tr> <td>data</td> </tr> <tr> <td> <a class="toggle-div">More Info</a> <div class="paragraph-div"> <p>Hidden text - Toggle more info to show.</p> </div> </td> </tr> </table> 

(see also this Fiddle ) (另请参阅此小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM