2013-07-26 5 views
0

나는 코드 아래에 있습니다div를 html 테이블의 행 아래에 표시 하시겠습니까?

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 


    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> 


    <style type="text/css"> 
     table { 
    table-layout: fixed; 
    width: 100%; 
    border-collapse: collapse; 
    } 

td {border: 1px solid #000; padding: 5px; vertical-align: top;} 

    </style> 


    <script type="text/javascript"> 
     $(document).ready(function() { 
     $('#content1').hide(); 
     $('#content2').hide(); 
     $('#content3').hide(); 


$("input").click(function() { 
var thisRow = $(this).closest('tr'); //parent row of the input or whatever is the click trigger 
var conDiv = $(this).data("href"); //returns #content1 - id of the content div you want shown 
var conH = $(conDiv).height(); //corresponding content div height 
var rowH = $(thisRow).height(); // this row height 
var newrowH = conH + rowH; //the new row height 
var posL = $(thisRow).position().left; // left position that div needs to be 
var posT = $(thisRow).position().top + rowH + 5 + 'px'; // top position that div needs to be ** not working properly!! 


    if ($(conDiv).is(":visible")) { 
     $(thisRow).css({"height" : "auto"}); 
     $(conDiv).css({"display" : "none", "left": "auto", "top" : "auto" }); 
    } else { 
     //reset all rows to normal height 
     $("tr").css({"height" : "auto"}); 
     //expand the height of this row 
     $(thisRow).css({"height" : newrowH}); 

     // reset all content divs.. hide them    
     $("[id*=content]").css({"display" : "none", "left": "auto", "top" : "auto"}); 
     //show the one you want 
     $(conDiv).css({"display" : "block", "left": posL, "top" : posT}); 
    } 
}); 

}); 
</script> 

</head> 
<body> 
<div id="table-wrap"> 
<table summary="test" cellspacing="0" id="master"> 
    <colgroup> 
     <col width="40px"> 
     <col span="4" width="25%"> 
    </colgroup> 

<thead> 
    <tr> 
    <th>&nbsp;</th> 
    <th><span>Customer</span></th> 
    <th><span>OrderID</span></th> 
    <th><span>Order date</span></th> 
    <th><span>Order Total</span></th> 
    </tr> 
</thead> 

<tbody> 
    <tr> 
     <td><input type="button" name="" value=" " data-href="#content1" ></td> 
     <td>Ernst Handel</td><td>10258</td><td>07/17/1996</td><td>$403.72</td> 
    </tr> 

    <tr> 
     <td><input type="button" name="" value=" " data-href="#content2"></td> 
     <td>Wartian Herkku</td><td>10270</td><td>08/01/1996</td><td>$1,376.00</td> 
    </tr> 

    <tr> 
     <td><input type="button" name="" value=" " data-href="#content3"></td> 
     <td>Magazzini Alimentari Riuniti</td><td>10275</td><td>08/07/1996</td><td>$15.36</td> 
    </tr> 

    </tbody> 
</table> 

<div id="content1"> 
<h2>content for row 1</h2> 
<table> 
    <thead> 
     <tr> 
      <th>head 1</th><th>head 2</th><th>head 3</th><th>head 4</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>cell 1</td><td>cell 2</td><td>cell 3</td><td>cell 4</td> 
     </tr> 
    </tbody> 
</table> 
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> 
</div><!-- content1 --> 

<div id="content2"> 
<h2>content for row 2</h2> 
<table> 
    <thead> 
     <tr> 
      <th>head 1</th><th>head 2</th><th>head 3</th><th>head 4</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>cell 1</td><td>cell 2</td><td>cell 3</td><td>cell 4</td> 
     </tr> 
    </tbody> 
</table> 
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> 
</div><!-- content2 --> 
<div id="content3"> 
<h2>content for row 3</h2> 
<table> 
    <thead> 
     <tr> 
      <th>head 1</th><th>head 2</th><th>head 3</th><th>head 4</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>cell 1</td><td>cell 2</td><td>cell 3</td><td>cell 4</td> 
     </tr> 
    </tbody> 
</table> 
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> 
</div><!-- content3 --> 
</div><!-- table-wrap --> 

</body> 
</html> 

사업부 버튼의 클릭에 행 아래에 표시해야한다. 아래 표와 같이 표시됩니다.

enter image description here

그러나로 표시되어야합니다 :

enter image description here

저를 도와주세요.

감사합니다. 당신이 당신의 테이블 위의 "콘텐츠 #"div의 위치를 ​​원하는 경우

+2

참고 사항 : Java! = Javascript! –

+2

테이블을 분할하지 않아도됩니다. 이 경우 모든 열 사이에 열 길이를 나타내는 빈 행을 지정할 수 있습니다. [방법 - DIV- 테이블 간 간] (http://www.sitepoint.com/forums/showthread.php?517923-how-to-put-DIV-between-table-rows) –

답변

4

간단한 & 문제를 효과적으로 해결했습니다. 당신은 추가하고 사업부 이런 식으로 데모 봐주세요을 제거 할 수 fiddle

$(document).delegate('input[type="button"]','click',function(){ 
    $('[colspan="5"]').parent('tr').remove(); 
    $(this).parents('tr').after('<tr/>').next().append('<td colspan="5"/>').children('td').append('<div/>').children().css('background','#f0f0f0').html($('#content').html()); 
}); 
+0

@RupamDatta 어쨌든 div가 행에서 아래로 슬라이드 할 수 있습니까? – Gofilord

+0

@Gofilord 여기가 당신을위한 해결책입니다. http://jsfiddle.net/ardeezstyle/4FKCa/121/ –

0
var $c=$(conDiv); 

$(this).parent().parent().after("<tr><td colspan='5'>"+$c.html()+"</td></tr>"); 
+0

add 그것은 '$ (conDiv) .css ({ "display": "block", "left": posL, "top": posT});'와'conDiv'에서 id 자체가 아닌 div 자체를 얻습니다. 이제는 condiv가 id라는 것을 알았습니다 – rps

+0

예.하지만 문제는 행의 높이가 증가하고 있습니다. – user1016403

+0

'$ (this) .parent(). parent(). after ("

my div
"); 이것은 div를 추가합니까 ?? – rps

0

당신은 여기 top:

0

간단하고 작은 스크립트는, 데모를 만들기 위해 그들에게 position:absolute를 추가해야 http://jsfiddle.net/yeyene/JyfPg/1/

$(document).ready(function() { 
    $('#content1').hide(); 
    $('#content2').hide(); 
    $('#content3').hide(); 

    $("input").click(function() { 
     if ($('tr#' + $(this).data("href")).is(":visible")) { 
      $('tr#' + $(this).data("href")).remove(); 
     } else { 
      $(this).closest('tr').after('<tr id="' + $(this).data("href") + '"><td colspan="5">' + $('#' + $(this).data("href")).html() + '</td></tr>'); 
     }      
    }); 

}); 
+0

답장을 보내 주셔서 감사합니다. 나는 그것을 시험해 보았다. 하지만 행을 숨길 때 행 사이의 공간이 계속 증가합니다. 단추를 여러 번 클릭하십시오. u가 문제를 보게됩니다. 답장을 보내 주셔서 감사합니다 – user1016403

+0

이 데모를 사용해 보셨습니까? http://jsfiddle.net/yeyene/JyfPg/1/ hide() 대신 remove()로 변경되었습니다. – yeyene

+0

바이올린에서 작동 중입니다. html 파일에 코드를 복사하면 작동하지 않습니다 ... – user1016403

0
<table> 
    <tr> 
     <td><input class="btn" type="button" value="detail"></td> 
     <td> 
      first row 
     </td> 
    </tr> 
<tr> 
     <td><input class="btn" type="button" value="detail"></td> 
    <td>second row</td> 
    </tr> 
</table> 



$(document).ready(function(){ 
    $('.btn').click(function(){ 
     var $current=$(this).parent().parent().index(); 
     if($(this).hasClass('clicked')) 
     { 
      var next=$current+1; 
      $('table tr:eq('+next+')').remove(); 
      $(this).removeClass('clicked'); 
     }else{ 
     $(this).addClass('clicked'); 
    var $current= $(this).parent().parent().index(); 
     $("table tr:eq("+$current+")").after("<tr><td colspan='2'><div>AddedDiv</div></td></tr>"); 
     } 
    }); 
}); 

에서 참조하시기 바랍니다. 감사합니다 데모 : http://jsfiddle.net/zeewon/GpvT8/

관련 문제