Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
3080 connectés 

  FORUM HardWare.fr
  Programmation
  HTML/CSS

  IE n'applique pas les styles a certaines div créées dynamiquement

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

IE n'applique pas les styles a certaines div créées dynamiquement

n°1432873
berzehk
Posté le 29-08-2006 à 09:23:06  profilanswer
 

Bonjour =)
 
Voila, j'aimerais comprendre s'il s'agit d'une erreur de ma part ou si c'est un bug.
Je suis en train de créer un modèle de box pour un site web. Ce modele marche parfaitement sur FF, mais sous IE, seules les boites créées statiquement -  i.e. dont le code etait deja dans la page HTML - s'affichent correctement.
 
voila le code source d'une box créée statiquement :

Code :
  1. <div id="testBox" class="Box"  style="width: 200px;">
  2. <div id="testBox_container" class="BoxContainer">
  3.  <div id="testBox_top" class="BoxTop">
  4.   <img src="./img/tl.gif" class="BoxTopLeftCorner" width="15 px" height="15 px" />
  5.   <img src="./img/ajax-loader.gif" class="BoxLoadingPic" alt="loading" width="15 px" height="15 px" />
  6.   <img src="./img/filler.gif" width="124 px" class="TopFiller" />
  7.   <img src="./img/errorPic.gif" class="BoxErrorPic" alt="error" width="15 px" height="15 px" />
  8.   <img src="./img/tr.gif" class="BoxTopRightCorner" width="15 px" height="15 px" />
  9.  </div>
  10.  <div  style="width: 200px;" id="testBox_content" class="BoxContent">
  11.   Box créée statiquement.
  12.  </div>
  13.  <div id="testBox_bottom" class="BoxBottom">
  14.   <img src="./img/bl.gif" class="BoxBottomLeftCorner" width="15 px" height="15 px" />
  15.   <img src="./img/filler.gif" width="162 px" class="BottomFiller" />
  16.   <img src="./img/br.gif" class="BoxBottomRightCorner" width="15 px" height="15 px" />
  17.  </div>
  18. </div>
  19. </div>


 
avec, en code CSS:

Code :
  1. .BoxContent{
  2.   background-color: #ffffcc;
  3. }
  4. .Box{
  5. margin : 10px;
  6. }
  7. body {
  8. background-color : #C2E678;
  9. }
  10. .BoxContainer {
  11.   background-color: #ffffcc;
  12. }
  13. .BoxLoadingPic{
  14. margin : 0px;
  15. padding : 0px;
  16. display : inline;
  17. vertical-align: top;
  18. }
  19. .BoxErrorPic{
  20. margin : 0px;
  21. padding : 0px;
  22. display : inline;
  23. vertical-align: top;
  24. background-color: #ffffcc;
  25. }
  26. .BoxTop{
  27. background-color: #ffffcc;
  28. width: 100%;
  29. }
  30. .TopFiller{
  31. vertical-align: top;
  32. margin : 0px;
  33. padding : 0px;
  34. display : inline;
  35. height : 15px;
  36. }
  37. .BottomFiller{
  38. vertical-align: bottom;
  39. margin : 0;
  40. padding : 0;
  41. display : inline;
  42. height : 15px;
  43. }
  44. .BoxBottom{
  45. background-color: #ffffcc;
  46. width: 100%;
  47. }
  48. .BoxTopLeftCorner{
  49. margin : 0;
  50. padding : 0;
  51. display : inline;
  52. vertical-align: top;
  53. }
  54. .BoxTopRightCorner{
  55. margin : 0px;
  56. padding : 0px;
  57. display : inline;
  58. vertical-align: top;
  59. }
  60. .BoxBottomLeftCorner{
  61. margin : 0px;
  62. padding : 0px;
  63. display : inline;
  64. vertical-align: bottom;
  65. }
  66. .BoxBottomRightCorner{
  67. margin : 0px;
  68. padding : 0px;
  69. display : inline;
  70. vertical-align: bottom;
  71. }


 
Maintenant, je demande a Javascript de créer la boite, avec le script suivant :

Code :
  1. function bodyOnLoadCallback()
  2. {
  3.  var Param = new Param1("200" );
  4. var root = document.getElementsByTagName("body" )[0];
  5. var doc = creerDiv("doc","doc",null);
  6. placerDiv(doc,root);
  7. var loginBox = createDivPart("loginBox","box créée dynamiquement.",doc,Param);
  8. }
  9. function creerDiv(idName,className,content)
  10. {
  11. var handler = document.createElement("div" );
  12. if (idName != null)
  13.             handler.setAttribute("id",idName);
  14. if (className != null)
  15.             handler.setAttribute("class",className);
  16.     if (content != null)
  17.             handler.innerHTML = content;
  18. return handler;
  19. }
  20. function createAndPlaceDiv(idName,className,content,target)
  21. {
  22. var res = creerDiv(idName,className,content);
  23. placerDiv(res,target);
  24. return res;
  25. }
  26. function placerDiv(handler, target)
  27. {
  28. target.appendChild(handler);
  29. }
  30. function createDivPart(boxId,content,root,style)
  31. {
  32. //   handler                  (id,class,content)  
  33. var boxHndlr = creerDiv(boxId,style.Box.CSSclass,null);
  34. //      (handler,target)
  35. boxHndlr.setAttribute("style","width: "+style.width+"px ;" );
  36. placerDiv(boxHndlr,root);
  37. //container
  38. boxHndlr.container = createBoxContainer(boxId,boxHndlr,style);
  39. //top
  40. boxHndlr.container.top = createBoxTop(boxId,boxHndlr.container,style);
  41. //content
  42. boxHndlr.container.content = createBoxContent(boxId,content,boxHndlr.container,style);
  43. //bottom
  44. boxHndlr.container.bottom = createBoxBottom(boxId,boxHndlr.container,style);
  45. return boxHndlr;
  46. }
  47. /* crée le container avec les parametres standards de class et id  */
  48. function createBoxContainer(boxIdParam,boxHandler,styleParam)
  49. {
  50. var result = createAndPlaceDiv(boxIdParam+styleParam.Box.Container.id,styleParam.Box.Container.CSSclass,null,boxHandler);
  51. return result;
  52. }
  53. function createBoxTop(boxIdParam,boxHandler,styleParam)
  54. {
  55. var result = createAndPlaceDiv(boxIdParam+styleParam.Box.Top.id,styleParam.Box.Top.CSSclass,styleParam.Box.Top.content,boxHandler);
  56. return result;
  57. }
  58. function createBoxContent(boxIdParam,contentParam,boxHandler,styleParam)
  59. {
  60. var result = createAndPlaceDiv(boxIdParam+styleParam.Box.Content.id,styleParam.Box.Content.CSSclass,contentParam,boxHandler);
  61. result.setAttribute("style","width : "+styleParam.width+"px ;" );
  62. return result;
  63. }
  64. function createBoxBottom(boxIdParam,boxHandler,styleParam)
  65. {
  66. var result = createAndPlaceDiv(boxIdParam+styleParam.Box.Bottom.id,styleParam.Box.Bottom.CSSclass,styleParam.Box.Bottom.content,boxHandler);
  67. return result;
  68. }
  69. function Param1(widthZ)
  70. {
  71. this.width = widthZ;
  72. this.Box = new Box1(widthZ);
  73. this.Text = new Text1();
  74. }
  75. function Text1()
  76. {
  77. this.LoginSubmitFunction = "java script:login()";
  78. this.LoginTitle = "Login";
  79. this.Logincontent = "<img src=\"iconeCle.jpg\" alt=\"\" width=\"15\" height=\"15\" /> Login <br /> <br />"+
  80.                       "   <form name=\"authentication_form\"  action=\"\" method=\"\" onsubmit=\"return false;\">"+
  81.        "<input type=\"text\" name=\"id_field\" value=\"ID\" width=\"45\" /><br />"+
  82.        "<input type=\"password\" name=\"password_field\" value=\"password\" width=\"45\" /><br /><br />"+
  83.        "<input type=\"submit\" value=\"OK\" name=\"send_button\" onClick=\""+this.LoginSubmitFunction+"\" /></form>";
  84. this.LoginErrorText = "Erreur : code ";
  85. }
  86. function Box1(widthY)
  87. {
  88. this.CSSclass="Box";
  89. this.Container = new Container1();
  90. this.Content = new Content1();
  91. this.Top = new Top1(widthY);
  92. this.Bottom = new Bottom1(widthY);
  93. }
  94. function Bottom1(widthX)
  95. {
  96. this.CSSclass = "BoxBottom";
  97. this.id = "_bottom";
  98. this.content = "<img src=\"./img/bl.gif\" class=\"BoxBottomLeftCorner\" width=\"15 px\" height=\"15 px\" /><img src=\"./img/filler.gif\" height=\"15px\" width=\""+(widthX-30)+" px\" class=\"BottomFiller\" /><img src=\"./img/br.gif\" class=\"BoxBottomRightCorner\" width=\"15 px\" height=\"15 px\" />"
  99. }
  100. function Top1(widthX)
  101. {
  102. this.CSSclass = "BoxTop";
  103. this.id = "_top";
  104. this.content = "<img src=\"./img/tl.gif\" class=\"BoxTopLeftCorner\" width=\"15 px\" height=\"15 px\" /><img src=\"./img/ajax-loader.gif\" class=\"BoxLoadingPic\" alt=\"loading\" width=\"15 px\" height=\"15 px\" /><img src=\"./img/filler.gif\" width=\""+(widthX-60)+" px\" class=\"TopFiller\" /><img src=\"./img/errorPic.gif\" class=\"BoxErrorPic\" alt=\"error\" width=\"15 px\" height=\"15 px\" /><img src=\"./img/tr.gif\" class=\"BoxTopRightCorner\" width=\"15 px\" height=\"15 px\" />";
  105. }
  106. function Content1()
  107. {
  108. this.CSSclass = "BoxContent";
  109. this.id = "_content";
  110. }
  111. function Container1()
  112. {
  113. this.CSSclass = "BoxContainer";
  114. this.id = "_container";
  115. }


 
Pour la petite explication du code, il a un objet param qui en fait décrit quasiment tout le code HTML de la box et les fonctions autour se contentent d'ajouter ce code dans la page.
Ce code ne renvoie aucune erreur sur la console javascript de FF, et IE ne renvoie aucune erreur.
Et pourtant, le resultat n'est pas a la hauteur sur IE..
 
http://www.cafzone.net/ipb/index.php?act=Attach&type=post&id=1016
 
En fait, les divs de classe BoxContainer et BoxContent devraient avoir un background-color, mais il disparait. Et je n'arrive pas a svoir pourquoi. J'ai essayé de rajouter le code "style="background-color" directement dans la balise divv en utilisant la fonction setAttribute(), mais ca ne change  rien...
 
Des Idées?  :cry:

mood
Publicité
Posté le 29-08-2006 à 09:23:06  profilanswer
 

n°1432878
anapajari
s/travail/glanding on hfr/gs;
Posté le 29-08-2006 à 09:28:07  profilanswer
 

remplace:

Code :
  1. handler.setAttribute("class",className);


par

Code :
  1. handler.className = className;

n°1432908
berzehk
Posté le 29-08-2006 à 10:09:01  profilanswer
 

je viens d'essayer, ca ne marche plus sous FF (mais ca c'est pas grave), par contre, sous IE, l'erreur devient : objet attendu a la ligne

Code :
  1. <body onload="javascript:bodyOnLoadCallback()" >

n°1432936
anapajari
s/travail/glanding on hfr/gs;
Posté le 29-08-2006 à 10:49:09  profilanswer
 

Garanti le node.className fonctionne sur IE et FF.
 
Quelle est l'erreur dans la console de FF?
L'erreur sous IE laisse penser à un problème dans ton code.
Par ailleurs le "javascript:" est inutile dans les events.

n°1432985
berzehk
Posté le 29-08-2006 à 11:42:45  profilanswer
 

en fait j'suis  tout honteux :$ ca marche. Mais y'a encore un probleme avec une des divs qui prend toute la largeur de la page, j'vais voir comment jdois regler ca
Merci bcp  :hello:


Message édité par berzehk le 29-08-2006 à 11:43:00
n°1432993
berzehk
Posté le 29-08-2006 à 11:54:59  profilanswer
 

eeuh... en fait, je veux bien que tu me dises ce que je peux utiliser comme nodes sur IE, parce que le probleme se repose:
J'utilisais la fonction setAttribute pour appliquer  
style="width : "+boxWidth
mais ca ne marche pas sous IE, ce qui me donne mon probleme de largeur..  
 
T'as pas un site avec une reference complete par hasard? :$

n°1432996
anapajari
s/travail/glanding on hfr/gs;
Posté le 29-08-2006 à 12:01:25  profilanswer
 

C'est pas comme ça qu'on change le style d'un noeud :o

Code :
  1. node.style.attribut = savaleur;


Dans ton cas:

Code :
  1. node.style.width = boxWidth + 'px';


Et n'oublie pas l'unité c'est important.
 
Pour les correspondantes JS des propriétés CSS tu peux, par exemple, jeter un oeil la:
http://developer.mozilla.org/en/docs/DOM:CSS

Message cité 1 fois
Message édité par anapajari le 29-08-2006 à 12:03:55
n°1432999
gatsu35
Blablaté par Harko
Posté le 29-08-2006 à 12:04:51  profilanswer
 

anapajari a écrit :

C'est pas comme ça qu'on change le style d'un noeud :o

Code :
  1. node.style.attribut = savaleur;


Dans ton cas:

Code :
  1. node.style.width = boxWidth + 'px';


Et n'oublie pas l'unité c'est important.
 
Pour les correspondantes JS des propriétés CSS tu peux, par exemple, jeter un oeil la:
http://developer.mozilla.org/en/docs/DOM:CSS


On peut :o changer comme il fait mais faut passer par node.style.cssText :o

n°1433004
anapajari
s/travail/glanding on hfr/gs;
Posté le 29-08-2006 à 12:09:04  profilanswer
 

gatsu35 a écrit :

On peut :o changer comme il fait mais faut passer par node.style.cssText :o


Mais stfu n00b :o T'as lu mon post ou bien?
 
Et je vais citer la doc pour eclairer mon propos:
- http://developer.mozilla.org/en/docs/DOM:CSS

Citation :


In other words, the first of the following two constructions is bad, and the latter is better practice in the DOM:
bad
    element.style = "background-color: blue";  
good
    element.style.backgroundColor = "blue";


- http://developer.mozilla.org/en/docs/style

Citation :

It is generally better to use the style property than elt.setAttribute('style', '...') from a script, since use of the style property will not overwrite other CSS properties that may be specified in the style attribute.

n°1433013
gatsu35
Blablaté par Harko
Posté le 29-08-2006 à 12:35:28  profilanswer
 

Je sais tout ça :o, mais fallait bien que je parle pour rien dire :o


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  HTML/CSS

  IE n'applique pas les styles a certaines div créées dynamiquement

 

Sujets relatifs
Probleme de styles[Résolu] [C#.Net] Ecrire dynamiquement le contenu d'un <legend>
Utiliser les méthodes statiques dynamiquement[Résolu] [C#.Net] Ecrire du texte dynamiquement dans une page .aspx
JApplet : Mettre à jour un panel dynamiquementactiver/desactiver des trigger dynamiquement
Avoir deux styles d'images différentesProblème : le CSS ne s'applique pas à la page en HTML
[Fin] Gérer les évenements sur des boutons créés dynamiquementgerer les ancres dynamiquement
Plus de sujets relatifs à : IE n'applique pas les styles a certaines div créées dynamiquement


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)