简体   繁体   English

显示隐藏的div不起作用

[英]showing hiding div not working

Hi all I have the following html code 大家好,我有以下html代码

<div id="orderlist">
    <div class="panel" id="1">
        <div class="p_header">Order No: 1
            <input class="ordview" type="button" value="View Details"/>
            <input class="select" type="button" value="Add"/>
        </div>
        <div class="p-content" style="display:none;">
            <table>
                <tr><td>Contact Person:</td><td></td></tr>
                <tr><td>Telephone:</td><td></td></tr>
                <tr><td>E-Mail:</td><td></td></tr>
                <tr><td>Address:</td><td></td></tr>
                <tr><td></td><td></td></tr>
                <tr><td>Code</td><td></td></tr>
                <tr><td>City</td><td></td></tr>
            </table>
        </div>
    </div>
</div>

with the following jquery to toggle it but it is not working, cannot figure out why though 用下面的jQuery来切换它,但它不起作用,虽然不知道为什么

$("body").on('click',".ordview",function(){
   $(this).closest('div.p-content').css('display','block');
   alert('view button clicked');
});

.closest() goes up the DOM and p.content isn't an ancestor, but div.panel is. .closest()进入DOM, p.content不是祖先,而div.panel是祖先。 Use: 采用:

$("body").on('click', ".ordview", function () {
    $(this).closest('div.panel').find('div.p-content').toggle();
});

jsFiddle example jsFiddle示例

Try this code to show/hide details 尝试使用此代码显示/隐藏详细信息

  $(".ordview").bind('click', function () {
      $(this).parent().next().toggle();
  });  

I personally would use the toggle() command. 我个人将使用toggle()命令。 Below funciton works and can check jsFiddle 下面的功能有效,可以检查jsFiddle

$("body").on('click',".ordview",function(){
   $(this).closest('div.panel').children('div.p-content').toggle();
});

jsFiddle Example jsFiddle示例

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

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