par
lettucie » 17 sept. 2006, 23:32
Bonjour,
je suis à la fois débutant en javascript et profane en code, de manière generale. Je fais un site web et j'ai eu envie de faire une page où le paragraphe s'affiche seulement lorsque l'on clique sur le titre. Grace à un tuto (dont je n ai pas saisie toute la moelle
http://www.onlinetools.org/articles/uno ... ation.html ) j'ai réussi a faire cette page qui fonctionne
http://hydre.gp.free.fr/contenus/aube.php?
seulement, lorsque je l'insere dans la trame du site, visiblement les balises posent des problemes ( ici ce doit etre les balises div);
http://hydre.gp.free.fr/pages/aube.php
donc je ne comprends pas pourquoi et forcement je ne sais pas quoi faire.
le script que j'utilise est :
Code : Tout sélectionner
<script type="text/javascript">
function collapse()
{
// check if DOM is available, return if not
if(!document.createTextNode){return;}
// create new paragraph
var p=document.createElement('p');
// loop over all headlines of the second level
var heads=document.getElementsByTagName('h2');
for(var i=0;i<heads.length;i++)
{
// grab the next sibling (the loop is needed because
// of whitespace issues
var tohide=heads[i].nextSibling;
while(tohide.nodeType!=1)
{
tohide=tohide.nextSibling;
}
// hide the sibling by applying the class 'hidden' and
// show that the headline is clickable by applying
// the class 'trigger'
cssjs('add',tohide,'hidden')
cssjs('add',heads[i],'trigger')
// store the element to be hidden in an attribute
heads[i].tohide=tohide;
// add the class 'hover' when the mouse touches the headline
heads[i].onmouseover=function()
{
cssjs('add',this,'hover');
}
// remove the class 'hover' when the mouse leaves the headline
heads[i].onmouseout=function()
{
cssjs('remove',this,'hover');
}
// if the user activates the headline
heads[i].onclick=function()
{
// test if the class 'hidden' is already applied to the
// next sibling
if(cssjs('check',this.tohide,'hidden'))
{
// if that is the case, replace it with shown and
// the headline class with open
cssjs('swap',this,'trigger','open');
cssjs('swap',this.tohide,'hidden','shown');
} else {
// and vice versa
cssjs('swap',this,'open','trigger');
cssjs('swap',this.tohide,'shown','hidden');
}
}
// insert the new paragraph before the first h2.
document.body.insertBefore(p,document.getElementsByTagName('h2')[0]);
}
function cssjs(a,o,c1,c2)
{
switch (a){
case 'swap':
o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
break;
case 'add':
if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
break;
case 'remove':
var rep=o.className.match(' '+c1)?' '+c1:c1;
o.className=o.className.replace(rep,'');
break;
case 'check':
return new RegExp('\\b'+c1+'\\b').test(o.className)
break;
}
}
}
window.onload=collapse;
</script>
je rajoute le csss que j avais oublié
j'espère que quelqu un pourra m'aider, et je vous remercie d'avance
Code : Tout sélectionner
h2{
text-align: center;
font-size:110%;
font-weight:normal;
}
.hidden{
display:none;
}
.shown{
display:block;
}
.trigger{
background:transparent;
}
.open{
background:transparent;
color:10613e;
font-style: italic;
}
.hover{
background:transparent;
color:cdd9d9;
cursor:pointer;
}
Bonjour,
je suis à la fois débutant en javascript et profane en code, de manière generale. Je fais un site web et j'ai eu envie de faire une page où le paragraphe s'affiche seulement lorsque l'on clique sur le titre. Grace à un tuto (dont je n ai pas saisie toute la moelle http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html ) j'ai réussi a faire cette page qui fonctionne
http://hydre.gp.free.fr/contenus/aube.php?
seulement, lorsque je l'insere dans la trame du site, visiblement les balises posent des problemes ( ici ce doit etre les balises div);
http://hydre.gp.free.fr/pages/aube.php
donc je ne comprends pas pourquoi et forcement je ne sais pas quoi faire.
le script que j'utilise est :
[code]<script type="text/javascript">
function collapse()
{
// check if DOM is available, return if not
if(!document.createTextNode){return;}
// create new paragraph
var p=document.createElement('p');
// loop over all headlines of the second level
var heads=document.getElementsByTagName('h2');
for(var i=0;i<heads.length;i++)
{
// grab the next sibling (the loop is needed because
// of whitespace issues
var tohide=heads[i].nextSibling;
while(tohide.nodeType!=1)
{
tohide=tohide.nextSibling;
}
// hide the sibling by applying the class 'hidden' and
// show that the headline is clickable by applying
// the class 'trigger'
cssjs('add',tohide,'hidden')
cssjs('add',heads[i],'trigger')
// store the element to be hidden in an attribute
heads[i].tohide=tohide;
// add the class 'hover' when the mouse touches the headline
heads[i].onmouseover=function()
{
cssjs('add',this,'hover');
}
// remove the class 'hover' when the mouse leaves the headline
heads[i].onmouseout=function()
{
cssjs('remove',this,'hover');
}
// if the user activates the headline
heads[i].onclick=function()
{
// test if the class 'hidden' is already applied to the
// next sibling
if(cssjs('check',this.tohide,'hidden'))
{
// if that is the case, replace it with shown and
// the headline class with open
cssjs('swap',this,'trigger','open');
cssjs('swap',this.tohide,'hidden','shown');
} else {
// and vice versa
cssjs('swap',this,'open','trigger');
cssjs('swap',this.tohide,'shown','hidden');
}
}
// insert the new paragraph before the first h2.
document.body.insertBefore(p,document.getElementsByTagName('h2')[0]);
}
function cssjs(a,o,c1,c2)
{
switch (a){
case 'swap':
o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
break;
case 'add':
if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
break;
case 'remove':
var rep=o.className.match(' '+c1)?' '+c1:c1;
o.className=o.className.replace(rep,'');
break;
case 'check':
return new RegExp('\\b'+c1+'\\b').test(o.className)
break;
}
}
}
window.onload=collapse;
</script>[/code]
je rajoute le csss que j avais oublié
j'espère que quelqu un pourra m'aider, et je vous remercie d'avance[code] h2{
text-align: center;
font-size:110%;
font-weight:normal;
}
.hidden{
display:none;
}
.shown{
display:block;
}
.trigger{
background:transparent;
}
.open{
background:transparent;
color:10613e;
font-style: italic;
}
.hover{
background:transparent;
color:cdd9d9;
cursor:pointer;
}[/code]