在另一个 table 行中隐藏和显示 table

Hide and Show table inside a another table row

我有 table 正在动态生成:

tables.ejs

 <table>
    <% for(var d=0;d<docs.length;d++)
      {  %>
        <tr>
        <td>
            <img src="http://www.bls.gov/images/icons/icon_small_plus.gif" class="image1" 

              id="image1" onclick=diffImage(this) />
        </td>
        <td>
          <p><%= docs[d].name %></p></a>
        </td>
        <td>
          <%= docs[d].url_Address %>
        </td>
        <td>
          <%= docs[d].product_Name %></p></a>
        </td>
        </tr>
    <div id= "sub" class="sub">
        <tr class="sub" id="<%= docs[d]._id %>" >
            <table style="width:100%;font-size: 14px;" align="right" bgcolor="#A0A0A3" >
            <% for(var e=0;e<doc.length;e++)
              {   %>
                 <tr>
                    <td> 
                       <a href=<%= doc[e].url_Address %>><p><%= doc[e].product_Name %></p></a>
                    </td>
                    <td>
                    <p class = "tab"><%= doc[e].vendor_Name %></p>
                    </td>
                 </tr>
          <% } %>
           </table>
        </tr>
    </div>
    <% } %>
    </table>

现在我希望当我点击图像时图像会改变并且 div 应该隐藏

我尝试关注 jquery .

<script>
  function diffImage(img) {
   if(img.src.match("http://olenka.med.virginia")){
        img.src = "http://www.bls.gov/images/icons/icon_small_plus.gif";
        $(".image1").click(function(){
            $(".sub").hide();
         });
   }else{
        $(".image1").click(function(){
            $(".sub").show();
        });
        img.src = "http://olenka.med.virginia.edu/psi/images/icons/minus_icon.png";
        }
    }
</script>

它在点击图像时隐藏 div。

但问题是:

当我点击显示子 table 的图像时,它显示了每一行的所有子 table, 和试图隐藏行时一样?

我想当点击图像时只有它的子 table 被隐藏或显示而不是所有子 table ?

试试这个:

<script>
  function diffImage(img) {
   if(img.src.match("http://olenka.med.virginia")){
        img.src = "http://www.bls.gov/images/icons/icon_small_plus.gif";
        $(".image1").click(function(){
            $(this).closest('tr').next('.sub').hide();
         });
   }else{
        $(".image1").click(function(){
            $(this).closest('tr').next('.sub').show();
        });
        img.src = "http://olenka.med.virginia.edu/psi/images/icons/minus_icon.png";
        }
    }
</script>