简体   繁体   English

将表格标题放置在固定位置

[英]Position the header of the table in the fixed position

I wanted to position the header of the table in the fixed position. 我想将表的标题放置在固定位置。 the header of the table is somewhere near the bottom of the page. 表格的标题位于页面底部附近。 When the jquery get is succeeded, the header go up according to the data. jQuery get成功后,标题将根据数据上升。 So I want to position the header as a fixed position. 所以我想将标题放置在固定位置。

the output should be like this 输出应该是这样的 在此处输入图片说明

I tried modifying the position for the thead but it does not work. 我尝试修改thead的位置,但不起作用。

my code is found below... 我的代码在下面...

html code HTML代码

<table id="topfivecountry" style="color:white;font-size:15px;">
            <thead style="position:absolute;bottom:40%;right:3%;">
                <tr>
                    <th>Top 5 countries</th>
                </tr>
            </thead>
        </table>

js code js代码

$.get ({
        url:'getTopFiveCountry.php',
        dataType: 'text',
        success: getTopFive
    });



function getTopFive(val) {
        var countryArray = val.split('\n');
        //country is the argument that is being passed by the forEach to the callback function
        countryArray.forEach(function(country){
            $('#topfivecountry').append('<tr><td>'+country+'</td></tr>')
        });
    }

I think you can make the header position absoluted to the table, and set the table position: absolute; right: 3%; top: 60%; 我认为您可以使表头位置绝对到表,并设置表position: absolute; right: 3%; top: 60%; position: absolute; right: 3%; top: 60%; to near the bottom of the page. 到页面底部附近。

example like: 像这样的例子:

 $.get({ url:'getTopFiveCountry.php', dataType: 'text', success: getTopFive }); var $tbody = $('#topfivecountry tbody'); getTopFive('a\\nb\\nc\\n'); function getTopFive(val) { var countryArray = val.split('\\n'); //country is the argument that is being passed by the forEach to the callback function countryArray.forEach(function(country){ $tbody.append('<tr><td>'+country+'</td></tr>') }); } 
 #topfivecountry { width: 150px; background-color: red; position: absolute; top: 60%; right: 3%; padding-top: 30px; } .thead { height: 30px; position:absolute; top: 0; left: 0; } .content { } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="topfivecountry" style="color:white;font-size:15px;"> <thead class="thead" style=""> <tr> <th>Top 5 countries</th> </tr> </thead> <tbody class="content"> </tbody> </table> 

I think you should add a <tbody> tag in your HTML table and while appending specify the <tbody> and then append your <tr> . 我认为您应该在HTML表中添加<tbody>标记,并在追加时指定<tbody> ,然后追加<tr> Now in your code, I think the country is getting appended to <thead> and it's <tr> and that's why the header text is getting shifted. 现在在您的代码中,我认为国家/地区将被附加到<thead> ,并且是<tr> ,这就是标题文本被移动的原因。 Also remove style="position:absolute;bottom:40%;right:3%;" 同时删除style="position:absolute;bottom:40%;right:3%;" . Attached a working copy. 附上工作副本。

 $.get ({ url:'getTopFiveCountry.php', dataType: 'text', success: getTopFive }); getTopFive('a\\nb\\nc\\n'); function getTopFive(val) { var countryArray = val.split('\\n'); //country is the argument that is being passed by the forEach to the callback function countryArray.forEach(function(country){ $('#topfivecountry > tbody:last-child').append('<tr><td>'+country+'</td></tr>') }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="topfivecountry" style="font-size:15px;"> <thead > <tr> <th>Top 5 countries</th> </tr> </thead> <tbody> </tbody> </table> 

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

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