简体   繁体   English

单击按钮显示/隐藏元素

[英]Show/hide element on button click

I got table like this: 我得到这样的表:

<tr>
    <td class="title">Title</td>
    <td class="body">Body</td>
    <td class="any">Any text</td>
</tr>
<tr>
    <td class="title">Title</td>
    <td class="body">Body</td>
    <td class="any">Any text</td>
</tr>
<tr>
    <td class="title">Title</td>
    <td class="body">Body</td>
    <td class="any">Any text</td>
</tr>

I'm using this script to hide/show td="body" 我正在使用此脚本隐藏/显示td =“ body”

$('.title').append("<button class='moredrop'>&darr;</button>");
$(".moredrop").click(function(){
    $(".body").toggle();

It shows and hides all td's in table. 它显示和隐藏表中的所有td。 I want it to hide/show only this td like here: 我希望它像这样只隐藏/显示此td:

     $("tr").toggle(
    function(){$(".body",this).css("display","block");},
    function(){$(".body",this).css("display","none");});

But on button click. 但是在按钮上单击。 Any ideas? 有任何想法吗?

Search the containing TR and then go to the specific .body 搜索包含的TR,然后转到特定的.body

$('.moredrop').on('click', function () {
    $(this).closest('tr').find('.body').toggle();
});

Try this : 尝试这个 :

$(document).ready(function(){
    $('.title').append("<button class='moredrop'>&darr;</button>");
    $('tr').on('click','.moredrop',function(){
            $(this).parents('tr').children('.body').toggle();
    });
});

Here is the working demo . 这是工作示例

$('.moredrop').click(function(){
    $('td.body').toggle();
});

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

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