【问题标题】:Show/Hide values in table在表格中显示/隐藏值
【发布时间】:2013-03-01 07:17:52
【问题描述】:

我有一个这样的表格:

<table id="example">
<thead>
<tr>
    <th>Header1</th>
    <th>Header2</th>
    <th>Header3</th>
</tr>
</thead>

<tbody>
<tr>
   <td>some php to get custom field</td>
   <td>also custom field- the loop is above so this is working as it should</td>
   <td>
   <a href="#" class="showhide"> 
    <div class="more">
     .. the content I want to show when user clicks a class="showhide"
    </div>
   </a>
   </td>
<tr>
</tbody>
</table>

我尝试使用 jQuery 来实现,但没有任何反应。这是我试过的 jQuery:

$(function () {
    $('.showhide').click(function () {
        $(this).next().toggle()
    });
});

我也使用document.ready 实现了它,但仍然无法正常工作。有没有其他方法可以做到这一点?

我想要的只是将 div class="more" 的内容显示在表格中的行下方(并且在下一个之前),当然,当单击 class="showhide" 时。

我将它与插件 DataTables 一起使用,但这不会导致问题。

【问题讨论】:

    标签: javascript jquery jquery-plugins html-table show-hide


    【解决方案1】:

    你的班级是showmore 而不是showhide

    Live Demo

    $(function() {
      $('.showmore').click(function(){
        $(this).next().toggle()
      });
    });
    

    基于 OP cmets 进行编辑:我缩短了 html 以使其简单,并从 a 标记中取出 div。

    Live Demo

    HTML

    <table id="example">
        <tbody>
            <tr>
                <td> <a href="#" class="showhide"> Show / Hide </a>
    
                    <div class="more">.. the content I want to show when user clicks a class="showmore"</div>
                </td>
            </tr>
        </tbody>
    </table>
    

    Javascript

    $(function () {
        $('.showhide').click(function () {
            $(this).next().toggle()
        });
    });
    

    【讨论】:

    • 抱歉,我在输入问题时打错字了。仍然无法正常工作,问题仍然存在:)
    【解决方案2】:

    要么在标记中更改它:

     class="showmore"> 
    

    到这里:

     class="showhide"> 
    

    或者这样做:

    $('.showmore').click(function (e) {
        e.preventDefault();
        $(this).next().toggle()
    });
    

    注意:

    这是无效的 html:

    <a href="#" class="showhide"> 
    <div class="more">
     .. the content I want to show when user clicks a class="showhide"
    </div>
    

    改成这样:

    <a href="#" class="showmore">show more</a> 
    <div class="more">
     .. the content I want to show when user clicks a class="showhide"
    </div>
    

    或者你可以用.children()而不是.next()试试这个:

    $('.showmore').click(function (e) {
        e.preventDefault();
        $(this).children('more').toggle();
    });
    

    TRYOUT IN FIDDLE HERE

    【讨论】:

    • 对不起,我输入问题时打错了,否则课程是正确的,无法正常工作
    • 太棒了,我只需要准备好文档并切换其他 jquery 调用。谢谢杰!
    【解决方案3】:
    $(document).on('click','.showmore',function(){
    //your code..
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      相关资源
      最近更新 更多