24

次のコードに示す HTML テーブルがあります。これには 16 列あります。JavaScriptを使用してセル幅を設定しています。全体の幅が 100% 未満の場合、正常に動作しています。

幅の合計が100%を超えた場合に を表示したいhorizontal scroll bar。しかし、黄色で強調表示されたセルの幅を 5% から 30% に変更すると、スクロール バーが表示されず、残りのすべてのセルが非表示になります。

質問:

  1. 現在スクロールバーが表示されないのはなぜですか?
  2. を示すことで、適切に動作させるにはどうすればよいscroll barでしょうか? (黄色の列の後の 2 つの列visibleも非表示にしないでください)

注: この問題はIE8で指摘されています。

注:コードについては、 http: //jsfiddle.net/Lijo/dYSfN/2/を参照してください。これは問題を示していません。問題を視覚化するには、コードを html ファイルにコピーし、IE8 を使用して開いてください。

アップデート

一方hiding a column、残りの列幅は、Firefox と Chrome では表の幅に自動的に調整されますが、IE8 では調整されません。IE8 ではテーブル幅shrinks

http://srikanthgade.blogspot.in/2010/09/ie8-standards-table-layout-fixed.html#!/2010/09/ie8-standards-table-layout-fixed.html

問題の詳細

黄色で強調表示されたセルの幅が 5% の場合

ここに画像の説明を入力

黄色で強調表示されたセルの幅が 30% の場合

ここに画像の説明を入力

スタイル

   .gridTableBorder
   {
        overflow:scroll;
        border: 2px solid green;
   }

    /*GridView Tables*/
    .resultGridTable
    {
        table-layout: fixed; /*Needed along with word wrap */
    }

    .resultGridTable th
    {
        background-color: #A7A7A6;
        color: #ffffff;
        padding: 2px 5px 2px 5px;
        font: bold 9pt Arial;
        border: 1px solid red;
        word-wrap: break-word;
    }

    .resultGridTable td
    {
        padding: 0px 5px 0px 5px;
        font: normal 9pt Arial;
        word-wrap: break-word;
        border: 1px solid blue;
    }

ジャバスクリプト

    $(document).ready(function () {


        //Width Setting
        var numberOfColumns = 16;
        $('.resultGridTable th, .resultGridTable td').each(function (i) {


            if (i % numberOfColumns == 0) {
                $(this).css('width', '1%');
            }

            if (i % numberOfColumns == 1) {
                $(this).css('width', '10%');
            }

            if (i % numberOfColumns == 2) {
                $(this).css('width', '9%');

            }

            if (i % numberOfColumns == 3) {
                $(this).css('width', '8%');
                $(this).css('background-color', 'orange');
            }

            if (i % numberOfColumns == 4) {
                $(this).css('width', '6%');
            }
            if (i % numberOfColumns == 5) {
                $(this).css('width', '8%');
            }
            if (i % numberOfColumns == 6) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 7) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 8) {
                $(this).css('width', '5%');
            }

            ///
            if (i % numberOfColumns == 9) {
                $(this).css('width', '7%');
            }
            if (i % numberOfColumns == 10) {
                $(this).css('width', '8%');
                $(this).css('background-color', 'orange');
            }

            if (i % numberOfColumns == 11) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 12) {
                $(this).css('width', '5%');
            }

            if (i % numberOfColumns == 13) {
                $(this).css('width', '30%');
                $(this).css('background-color', 'Yellow');
            }

            if (i % numberOfColumns == 14) {
                $(this).css('width', '7%');
            }

            if (i % numberOfColumns == 15) {
                $(this).css('width', '7%');
            }

        }
        );


        //Hide Is Summary Row Column
        var selectedElements = $("tr").find("th:first, td:first");
        $(selectedElements).hide();


    }
    );

HTML

<body>
<form method="post" action="LocalTaxReport.aspx" id="form1">
<div id="wrapper">
    <div id="container">
        <div id="centralContainer">
            <div id="mainContainer">
                <div id="contentHolderDiv" class="contentHolderDiv">
                    <div id="resultContainer" class="resultContainerDiv">
                        <div id="gridDiv" class="gridTableBorder">
                            <div>
                                <table class="resultGridTable" cellspacing="0" id="detailContentPlaceholder_grdLocalTaxReport"
                                    style="border-collapse: collapse;">
                                    <tr>
                                        <th scope="col">
                                            IsSummaryRow
                                        </th>
                                        <th scope="col">
                                            Associate
                                        </th>
                                        <th scope="col">
                                            My Amount
                                        </th>
                                        <th scope="col">
                                            Federal Withholding
                                        </th>
                                        <th scope="col">
                                            Social Security
                                        </th>
                                        <th scope="col">
                                            Medicaring
                                        </th>
                                        <th scope="col">
                                            State Tax
                                        </th>
                                        <th scope="col">
                                            County Tax
                                        </th>
                                        <th scope="col">
                                            City Tax
                                        </th>
                                        <th scope="col">
                                            Total
                                        </th>
                                        <th scope="col">
                                            State
                                        </th>
                                        <th scope="col">
                                            State Code
                                        </th>
                                        <th scope="col">
                                            County
                                        </th>
                                        <th scope="col">
                                            County Code
                                        </th>
                                        <th scope="col">
                                            City
                                        </th>
                                        <th scope="col">
                                            City Code
                                        </th>
                                    </tr>
                                    <tr>
                                        <td>
                                            False
                                        </td>
                                        <td>
                                            Mary Dryden
                                        </td>
                                        <td>
                                            $3450
                                        </td>
                                        <td>
                                            $32
                                        </td>
                                        <td>
                                            $5
                                        </td>
                                        <td>
                                            $2
                                        </td>
                                        <td>
                                            $10
                                        </td>
                                        <td>
                                            $1
                                        </td>
                                        <td>
                                            $2
                                        </td>
                                        <td>
                                            $3400
                                        </td>
                                        <td>
                                            Arkansas
                                        </td>
                                        <td>
                                            AR
                                        </td>
                                        <td>
                                            Benton
                                        </td>
                                        <td>
                                            04567
                                        </td>
                                        <td>
                                            Bentonville
                                        </td>
                                        <td>
                                            23156
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div class="clear">
                    </div>
                </div>
            </div>
            <div class="clear">
            </div>
        </div>
    </div>
</div>
</form>

 <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>

</body>
4

5 に答える 5

18

私はあなたが持っていた望ましい効果を少しの修正で達成しようとしました、そして私はそれをここで説明するつもりです。

  1. を設定する必要がありdoctypeます。現在のほとんどのマークアップ言語ではDOCTYPE宣言が必須であり、それがないと、ドキュメントを確実に検証したり、適用するルールを決定したりすることはできません。

  2. テーブルの幅が設定されていないため、スクロールバーは表示されません。divはデフォルトで。を取ります100%。次に、overflowプロパティをdivに設定してから、テーブルの幅を120%(divを超える100%)に設定して、スクロールバーを表示します。

  3. のテーブル幅は120%、すべての列幅を合計できることを意味するわけではありません120。、以降、テーブル幅を。に設定した場合でもtable-layout:fixed、すべての列幅の合計をに集計する必要があります。100120%

  4. コードでは、すべての列と行の幅を設定していましたが、代わりにth、すべての行(テーブル全体)のすべての列に適用される最初の行にのみ幅を適用しました。

  5. if..if..ifを使用するようにコードを変更しましたif..else if..else if

注: 4と5は、既存のコードからの改善です。

デモ:http: //jsfiddle.net/FP7MF/2/embedded/result/

完全なコード:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
    <title>My Report </title>
    <style type="text/css">

        /*GridView Tables*/
        .resultGridTable
        {
            table-layout: fixed; 
            width: 130%;
        }

        .resultGridTable th
        {
            background-color: #A7A7A6;
            color: #ffffff;
            padding: 2px 5px 2px 5px;
            font: bold 9pt Arial;
            border: 1px solid red;
            word-wrap: break-word;
        }

        .resultGridTable td
        {
            padding: 0px 5px 0px 5px;
            font: normal 9pt Arial;
            word-wrap: break-word;
            border: 1px solid blue;
        }

        #gridDiv div { overflow: auto;  }

    </style>
</head>
<body>
    <form>
        <div id="wrapper">
            <div id="container">
                <div id="centralContainer">
                    <div id="mainContainer">
                        <div id="contentHolderDiv" class="contentHolderDiv">
                            <div id="resultContainer" class="resultContainerDiv">
                                <div id="gridDiv" class="gridTableBorder">
                                    <div>
                                        <table class="resultGridTable" cellspacing="0" id="detailContentPlaceholder_grdLocalTaxReport"
                                             style="border-collapse: collapse;">
                                           <tr>
                                              <th scope="col">IsSummaryRow</th>
                                              <th scope="col">Associate</th>
                                              <th scope="col">My Amount</th>
                                              <th scope="col">Federal Withholding</th>
                                              <th scope="col">Social Security</th>
                                              <th scope="col">Medicaring</th>
                                              <th scope="col">State Tax</th>
                                              <th scope="col">County Tax</th>
                                              <th scope="col">City Tax</th>
                                              <th scope="col">Total</th>
                                              <th scope="col">State</th>
                                              <th scope="col">State Code</th>
                                              <th scope="col">County</th>
                                              <th scope="col">County Code</th>
                                              <th scope="col">City</th>
                                              <th scope="col">City Code</th>
                                           </tr>
                                           <tr>
                                               <td>False</td>
                                               <td>Mary Dryden</td>
                                               <td>$3450</td>
                                               <td>$32</td>
                                               <td>$5</td>
                                               <td>$2</td>
                                               <td>$10</td>
                                               <td>$1</td>
                                               <td>$2</td>
                                               <td>$3400</td>
                                               <td>Arkansas</td>
                                               <td>AR</td>
                                               <td>Benton</td>
                                               <td>04567</td>
                                               <td>Bentonville</td>
                                               <td>23156</td>
                                            </tr>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="clear"></div>
                        </div>
                    </div>
                    <div class="clear"></div>
               </div>
            </div>
        </div>
    </form>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>

    <script type="text/javascript">


        $(document).ready(function () {


            //Width Setting
            var numberOfColumns = 16;
            $('.resultGridTable th').each(function (i) {

                if (i % numberOfColumns == 0) {
                    $(this).css('width', '1%');
                } else if (i % numberOfColumns == 1) {
                    $(this).css('width', '10%');
                } else if (i % numberOfColumns == 2) {
                    $(this).css('width', '9%');
                } else if (i % numberOfColumns == 3) {
                    $(this).css({'width': '8%', 'background-color': 'orange'});
                } else if (i % numberOfColumns == 4) {
                    $(this).css('width', '6%');
                } else if (i % numberOfColumns == 5) {
                    $(this).css('width', '8%');
                } else if (i % numberOfColumns == 6) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 7) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 8) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 9) {
                    $(this).css('width', '7%');
                } else if (i % numberOfColumns == 10) {
                    $(this).css({'width': '8%', 'background-color': 'orange'});
                } else if (i % numberOfColumns == 11) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 12) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 13) {
                    $(this).css({'width': '8%', 'background-color': 'Yellow'});
                } else if (i % numberOfColumns == 14) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 15) {
                    $(this).css('width', '5%');
                }

            });

            //Hide Is Summary Row Column
            var selectedElements = $("tr").find("th:first, td:first");
            $(selectedElements).hide();
        });

    </script>
</body>
</html>
于 2012-11-30T21:53:22.453 に答える
2

これは完全な解決策ではありませんが、テーブル幅を 120% に設定し、列幅をテーブル幅のパーセンテージに変更すると (合計が 100% になります)、目的の効果が得られます。

于 2012-11-30T11:11:36.077 に答える
1

テーブル レイアウトを IE8 で修正したままにしておく必要がある場合は、列を非表示にした後に以下の回避策を追加する必要があります。

:
:
$(selectedElements).hide();

// Add the workaround after the above line (or when hiding of columns is done).
$(".resultGridTable").attr("style","display:inline-table;");
window.setTimeout(function(){$(".resultGridTable").attr("style","");},0);

これは IE8 のバグですが、Microsoft は解決策について回答していないようです。情報を見つけたら、ここに投稿してください。

しかし、回避策を試しましたが、それでも目的の出力が得られません。

ここに追加したので、さらに研究開発に役立つ可能性があります。

参考文献:

http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/f265c5b1-a45d-4949-85b2-86a97a884dc1/

回避策:

table-layout:fixed を使用してテーブルで列が非表示になっている場合、IE8 は tbody または thead のサイズを変更しません。

http://srikanthgade.blogspot.in/2010/09/ie8-standards-table-layout-fixed.html

于 2012-12-01T13:15:16.493 に答える
1

私が間違っていたらoverflow:scroll;すみませんが、テーブルを含む div の css ルールとして追加することで解決できませんか?

于 2012-11-28T11:44:32.773 に答える
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
.gridTableBorder
   {
        overflow:scroll;
        border: 2px solid green;
   }

    /*GridView Tables*/
    .resultGridTable
    {
        /*table-layout: fixed;*/ /*Needed along with word wrap */
    }

    .resultGridTable th
    {
        background-color: #A7A7A6;
        color: #ffffff;
        padding: 2px 5px 2px 5px;
        font: bold 9pt Arial;
        border: 1px solid red;
        /*word-wrap: break-word;*/
    }

    .resultGridTable td
    {
        padding: 0px 5px 0px 5px;
        font: normal 9pt Arial;
        /*word-wrap: break-word;*/
        border: 1px solid blue;
    }
</style>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>
<script type="text/javascript">

$(document).ready(function () {


        //Width Setting
        var numberOfColumns = 16;
        $('.resultGridTable th, .resultGridTable td').each(function (i) {


            if (i % numberOfColumns == 0) {
                $(this).css('width', '1%');
            }

            if (i % numberOfColumns == 1) {
                $(this).css('width', '10%');
            }

            if (i % numberOfColumns == 2) {
                $(this).css('width', '9%');

            }

            if (i % numberOfColumns == 3) {
                $(this).css('width', '8%');
                $(this).css('background-color', 'orange');
            }

            if (i % numberOfColumns == 4) {
                $(this).css('width', '6%');
            }
            if (i % numberOfColumns == 5) {
                $(this).css('width', '8%');
            }
            if (i % numberOfColumns == 6) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 7) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 8) {
                $(this).css('width', '5%');
            }

            ///
            if (i % numberOfColumns == 9) {
                $(this).css('width', '7%');
            }
            if (i % numberOfColumns == 10) {
                $(this).css('width', '8%');
                $(this).css('background-color', 'orange');
            }

            if (i % numberOfColumns == 11) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 12) {
                $(this).css('width', '5%');
            }

            if (i % numberOfColumns == 13) {
                $(this).css('width', '30%');
                $(this).css('background-color', 'Yellow');
            }

            if (i % numberOfColumns == 14) {
                $(this).css('width', '7%');
            }

            if (i % numberOfColumns == 15) {
                $(this).css('width', '7%');
            }

        }
        );


        //Hide Is Summary Row Column
        var selectedElements = $("tr").find("th:first, td:first");
        $(selectedElements).hide();


    }
    );
</script>
</head>

<body>

<form method="post" action="LocalTaxReport.aspx" id="form1">
<div id="wrapper">
    <div id="container">
        <div id="centralContainer">
            <div id="mainContainer">
                <div id="contentHolderDiv" class="contentHolderDiv">
                    <div id="resultContainer" class="resultContainerDiv">
                        <div id="gridDiv" class="gridTableBorder">
                            <div>
                                <table class="resultGridTable" cellspacing="1" id="detailContentPlaceholder_grdLocalTaxReport" style="border-collapse: collapse;">
                                    <tr>
                                        <th scope="col">
                                            IsSummaryRow
                                        </th>
                                        <th scope="col">
                                            Associate
                                        </th>
                                        <th scope="col">
                                            My Amount
                                        </th>
                                        <th scope="col">
                                            Federal Withholding
                                        </th>
                                        <th scope="col">
                                            Social Security
                                        </th>
                                        <th scope="col">
                                            Medicaring
                                        </th>
                                        <th scope="col">
                                            State Tax
                                        </th>
                                        <th scope="col">
                                            County Tax
                                        </th>
                                        <th scope="col">
                                            City Tax
                                        </th>
                                        <th scope="col">
                                            Total
                                        </th>
                                        <th scope="col">
                                            State
                                        </th>
                                        <th scope="col">
                                            State Code
                                        </th>
                                        <th scope="col">
                                            County
                                        </th>
                                        <th scope="col">
                                            County Code
                                        </th>
                                        <th scope="col">
                                            City
                                        </th>
                                        <th scope="col">
                                            City Code
                                        </th>
                                    </tr>
                                    <tr>
                                        <td>
                                            False
                                        </td>
                                        <td>
                                            Mary Dryden
                                        </td>
                                        <td>
                                            $3450
                                        </td>
                                        <td>
                                            $32
                                        </td>
                                        <td>
                                            $5
                                        </td>
                                        <td>
                                            $2
                                        </td>
                                        <td>
                                            $10
                                        </td>
                                        <td>
                                            $1
                                        </td>
                                        <td>
                                            $2
                                        </td>
                                        <td>
                                            $3400
                                        </td>
                                        <td>
                                            Arkansas
                                        </td>
                                        <td>
                                            AR
                                        </td>
                                        <td>
                                            Benton
                                        </td>
                                        <td>
                                            04567
                                        </td>
                                        <td>
                                            Bentonville
                                        </td>
                                        <td>
                                            23156
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div class="clear">
                    </div>
                </div>
            </div>
            <div class="clear">
            </div>
        </div>
    </div>
</div>
</form>

</body>
</html>

私はちょうどあなたの問題を経験しました.私はあなたのコードとcssについていくつかの研究開発を行いました. table-layout:fixed と word-wrap:break-word; を削除しました。

これはIE8とIE7で問題を引き起こしていました。word-wrap:break-word が原因でセルの高さが増加しているスクリーン ショット 2。

他のブラウザでは問題なく動作していました。コードをコピーして、ブラウザでチェックインするだけです。table-layout:fixed および word-wrap css プロパティを使用せずにニーズを満たす場合は、これが解決策です。

@Lijo何か不足している場合はお知らせください。

于 2012-12-05T07:15:08.693 に答える