42 votes

Problème de largeur de cellule de tableau dans Internet Explorer 8 avec le jeu de colspan

J'ai la page HTML suivante:

 <?xml version="1.0" encoding="UTF-8"?>
<!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" lang="en" xml:lang="en">
<head>
    <title>A title</title>
</head>
<body>
    <table style="width: 700px; border: solid 1px green">
        <tr>
            <td style="border: solid 1px red;" colspan="2">A cell with a bunch of text.  The amount of text here increases the 'x' cell.<td>
        </tr>
        <tr>
            <td style="width: 100px; border: solid 1px purple;" >x</td>
            <td style="border: solid 1px blue;">Some sample text</td>
        </tr>
    </table>
</body>
</html>
 

Dans tous les navigateurs autres qu'Internet Explorer (8), la cellule avec le contenu "x" a une largeur de 100 px et la cellule adjacente remplit le reste du tableau. Dans Internet Explorer 8, il est un peu plus grand et sa taille varie en fonction de la quantité de texte dans la cellule avec colspan = "2". Existe-t-il un correctif pour ce bogue dans IE?

23voto

Luke Points 570

Ok, c'est de la pure folie. Ray réponse travaillé pour le test initial, mais pas pour mon exemple réel, ce qui m'a amené à créer un deuxième cas de test avec les Rayons du corrigé:

<?xml version="1.0" encoding="UTF-8"?>
<!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" lang="en" xml:lang="en">
<head>
    <title>title</title>
</head>
<body> 

<form>

    <table style="width: 700px;">
        <tr>
            <td colspan="2">Here is some really long text.  For some reason, in internet explorer 8, the long text in a table cell that spans two columns above some other cells affects the cell below it. I have no idea why.  Also, if the contents of the first cell below this one is some text rather than a checkbox, then the effect is not as dramatic, though it's still there.</td>
        </tr>
        <tr>
            <td style="width:100px; background: green;"><input type="checkbox" value="1" /></td>
            <td style="width:600px;">blah</td>
        </tr>
    </table>

    <table style="width: 700px;" border="0">
        <tr>
            <td colspan="2">Some short text</td>
        </tr>
        <tr>
            <td style="width: 100px; background: red;"><input type="checkbox" value="1" /></td>
            <td style="width: 600px;">blah</td>
        </tr>
    </table>

</form>

</body>
</html>

Pour une raison quelconque, avoir les éléments d'entrée dans la première cellule du tableau, fait des Rayons de la solution de ne pas assez travailler.

La solution qui a eu pour résoudre le cas réel, que nous avons essayé de fixer nécessaire d'ajouter "table-layout:fixe" aux tables, et le premier rang dans le tableau ont des cellules vides de la largeur de l'ensemble. Voici une version modifiée de l'exemple précédemment avec le correctif que je viens de décrire:

<?xml version="1.0" encoding="UTF-8"?>
<!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" lang="en" xml:lang="en">
<head>
    <title>title</title>
</head>
<body> 

<form>

    <table style="table-layout: fixed; width: 700px;">
        <tr>
            <td style="width: 100px;"></td>
            <td style="width: 600px;"></td>
        </tr>
        <tr>
            <td colspan="2">Here is some really long text.  For some reason, in internet explorer 8, the long text in a table cell that spans two columns above some other cells affects the cell below it. I have no idea why.  Also, if the contents of the first cell below this one is some text rather than a checkbox, then the effect is not as dramatic, though it's still there.</td>
        </tr>
        <tr>
            <td style="background: green;"><input type="checkbox" value="1" /></td>
            <td>blah</td>
        </tr>
    </table>

    <table style="width: 700px; table-layout: fixed;" border="0">
        <tr>
            <td style="width: 100px;"></td>
            <td style="width: 600px;"></td>
        </tr>
        <tr>
            <td colspan="2">Some short text</td>
        </tr>
        <tr>
            <td style="background: red;"><input type="checkbox" value="1" /></td>
            <td>blah</td>
        </tr>
    </table>

</form>

</body>
</html>

Vraiment Internet Explorer??? VRAIMENT?

13voto

Libor Skočík Points 116

Voici mon résultat pour les échecs de td largeur dans IE8:

 <table style="width: 100%;" border="1">
  <tr>
    <td style="width:auto;">td1</td>
    <td style="width:15px;">td2</td>
    <td style="width:20px;">td3</td>
  </tr>
  <tr>
    <td colspan="3" style="width:auto;">ds fasdf asdf asdf asdf asfd asf asdf asdf adf sadf asdf asdf asdf asdf asdf asfasdf dasf sdaf asd</td>
  </tr>
</table>

<!-- IE8 HACK  100% WIDTH -->
<table style="width: 100%;" border="1">
  <tr>
    <td style="width:100%;">td1</td>
    <td style="width:15px;">td2</td>
    <td style="width:20px;">td3</td>
  </tr>
  <tr>
    <td colspan="3" style="width:auto;">ds fasdf asdf asdf asdf asfd asf asdf asdf adf sadf asdf asdf asdf asdf asdf asfasdf dasf sdaf asd</td>
  </tr>
</table>
 

4voto

ogradyjd Points 161

La réponse de Luke avec le "tableau-layout: fixed" est ce qui l'a finalement fait pour moi. Sans "table-layout: fixed", IE a continué à ignorer les déclarations de largeur explicites pour les cellules non colspan. Avec elle, tout fonctionnait bien, même s'il fallait indiquer explicitement à toutes les cellules non colspan quelle était leur largeur au lieu de simplement s'écouler à gauche, comme tout autre navigateur de la planète.

Score final: Luke: +1 IE: Suck it.

1voto

Sanath Nair Points 11

Utilisez la fonction setAttribute . Attribut Name = "colSpan" . Cela fonctionne sur IE 8 et Firefox également.
Exemple :

 cell.setAttribute("colSpan", 9);
 

0voto

Ray Points 12711

Puisque vous connaissez la taille de la table (700), vous pouvez contourner le bogue en définissant la largeur des deux cellules dans la deuxième ligne. Avec la deuxième cellule définie à 600 pixels, le tableau se comporte.

Mais ça craint toujours.

Modifier - voici une autre solution de contournement - ajoutez une image vide à la première cellule pour forcer la taille, puis ajoutez une largeur de 100% à la cellule xsecond:

 <tr> 
  <td style="width: 100px; border: solid 1px purple;" >x<img src="\images\blank.gif" width="100" height="1" /></td> 
  <td style="border: solid 1px blue;width:100%;">Some sample text</td> 
</tr> 
 

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X