简体   繁体   中英

Second td above first td + html table

I have a table like this:

<table>
    <tbody>
        <tr></tr>
        <tr></tr>
        <tr class="TableContent">
            <td class="shouldbeunderTableMenu"></td>
            <td class="TableMenu"></td>
        </tr>
    </tbody>
</table>

As you can see I have a td with class equal to TableMenu . I would like to position the td above the td with class shouldbeunderTableMenu .

So that the first td is vertically positioned under the second td. Image to clarify:

在此处输入图片说明

But how can I do this?

Try to use before ,

$(".shouldbeunderTableMenu").before($(".TableMenu"));

 $(".shouldbeunderTableMenu").before($(".TableMenu")); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr></tr> <tr></tr> <tr class="TableContent"> <td class="shouldbeunderTableMenu">1</td> <td class="TableMenu">2</td> </tr> </tbody> </table> 

or use after

$(".TableMenu").after($(".shouldbeunderTableMenu"));

 $(".TableMenu").after($(".shouldbeunderTableMenu")); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr></tr> <tr></tr> <tr class="TableContent"> <td class="shouldbeunderTableMenu">1</td> <td class="TableMenu">2</td> </tr> </tbody> </table> 

With jQuery:

$('td.TableMenu').each(function() { $(this).prependTo($(this).parent()); });

Or more generally:

$('td.TableMenu').each(function() { $(this).before($(this).siblings('.shouldbeunderTableMenu')); });

Why use all that fancy jQuery when you can use css :P

td{
    border:1px solid black;
    display:block;    
}
.TableMenu{
    margin-top:-45px;    
}
.shouldbeunderTableMenu{
    margin-top:25px;
}

Working example: http://jsfiddle.net/h0pLe8cw/1/

I would do this:

$(document).ready(function() {
    var shoulder  =  $(".TableContent .shouldbeunderTableMenu");
    var clone = shoulder.clone();
    shoulder.remove();
    $(".TableContent").append(clone);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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