IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Contrôle de CSS avec un bouton


Sujet :

JavaScript

  1. #1
    Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2013
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2013
    Messages : 75
    Points : 50
    Points
    50
    Par défaut Contrôle de CSS avec un bouton
    Bonsoir,
    je cherche à pouvoir contrôler le style CSS de mon texte avec un bouton, c'est à dire que lorsque je clic sur le bouton "annotation", tout le texte devient noir et de même taille, et si l'on re-clic, le style revient.
    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8" />
    		<title>Speak fast</title>
            <script type="text/javascript">
                var interID;
                var x = 0;
                var nbDiv = 10;
                var temps = 2500;
                window.onload = function()
                {
     
                }
                function surligne()
                {
                    //if (x == nbDiv)x=0;
                    for (i=0; i<nbDiv; i++)
                    {
    					document.getElementById("div"+(i+1)).style.backgroundColor = "white";
                    }
                    document.getElementById("div"+(x+1)).style.backgroundColor = '#7FFFD4';
                    if (x==1)temps=3400;//like
                     if (x==2)temps=3400;//You Know
                     if (x==3)temps=1500;//Math
                     if (x==4)temps=2000;//I really
                     if (x==5)temps=3000;
                     if (x==6)temps=2500;//any
                    if (x==7)temps=2800;
                    if (x==8)temps=1800;//because
                    if (x==9) temps=3000;
                    clearInterval(interID);
                    interID = setInterval(function()
                                          {  surligne();}, temps);
                    x++;
                }
                function stop()
                { clearInterval(interID); }
    			</script>
     
    		<!-- lecteur audio -->
    		<div id="divPlayer">
    			<h1>Audio-Player</h1>
     
    			<audio id="audioPlayer" src="atschool.wav" ontimeupdate="update(this)"></audio></br>
     
    			<div>
    				<div id="progressBarControl">
    					<div id="progressBar" onclick="clickProgress('audioPlayer', this, event)">Pas de lecture</div>
    				</div>
    				<span id="progressTime">00:00</span>
    			</div>
     
    			<button class="control" onclick="play('audioPlayer', this)">Play</button>
    			<button class="control" onclick="resume('audioPlayer')">Stop</button>
    			<button class="control" onclick="annotation('audioPlayer', this)">Annotation</button></br></br>
     
    			<span class="volume">
    				<a class="stick1" onclick="volume('audioPlayer', 0)"></a>
    				<a class="stick2" onclick="volume('audioPlayer', 0.3)"></a>
    				<a class="stick3" onclick="volume('audioPlayer', 0.5)"></a>
    				<a class="stick4" onclick="volume('audioPlayer', 0.7)"></a>
    				<a class="stick5" onclick="volume('audioPlayer', 1)"></a>
    			</span>
            </div></br>
     
            <script type="text/javascript">
     
    			/*function annotation(idPlayer, control) {
    				var player = document.querySelector('#' + idPlayer);
     
    				if (player.annotation) {
    					player.annotation();
    					control.textContent = 'Annotation';
    					document.getElementByClassName("syllabe").style.color= "black";
    					document.getElementByClassName("syllabe").style.fontSize= 18px;
    					document.getElementByClassName("syllabehaute").style.color= "black";
    					document.getElementByClassName("syllabehaute").style.fontSize= 18px;
    					document.getElementByClassName("syllabebasse").style.color = "black";
    					document.getElementByClassName("syllabebasse").style.fontSize = 18px;
    				} else {
    					control.textContent = 'Annoter';
    					document.getElementByClassName("syllabe").style.color= "red";
    					document.getElementByClassName("syllabe").style.fontSize= 16px;
    					document.getElementByClassName("syllabehaute").style.color= "blue";
    					document.getElementByClassName("syllabehaute").style.fontSize= 20px;
    					document.getElementByClassName("syllabebasse").style.color = '#FFA500';
    					document.getElementByClassName("syllabebasse").style.fontSize = 10px;
    				}
    			}*/
     
    			function play(idPlayer, control) {
                    var player = document.querySelector('#' + idPlayer);
     
                    if (player.paused) {
                        player.play();
                        interID = setInterval(function()
                                              { surligne();}, temps);
                        control.textContent = 'Pause';
                    } else {
                        player.pause();
                        clearInterval(interID);
                        control.textContent = 'Play';
                    }
                }
     
                function resume(idPlayer) {
                    var player = document.querySelector('#' + idPlayer);
                     clearInterval(interID);
                    for (i=0; i<nbDiv; i++)
                    {
    					document.getElementById("div"+(i+1)).style.backgroundColor = "white";
                    }
                    player.currentTime = 0;
                    player.pause();
     
                    x = 0;
                }
     
                function volume(idPlayer, vol) {
                    var player = document.querySelector('#' + idPlayer);
     
                    player.volume = vol;
                }
     
                function update(player) {
                    var duration = player.duration;    // Durée totale
                    var time     = player.currentTime; // Temps écoulé
                    var fraction = time / duration;
                    var percent  = Math.ceil(fraction * 100);
     
                    var progress = document.querySelector('#progressBar');
     
                    progress.style.width = percent + '%';
                    progress.textContent = percent + '%';
     
                    document.querySelector('#progressTime').textContent = formatTime(time);
                }
     
                function formatTime(time) {
                    var hours = Math.floor(time / 3600);
                    var mins  = Math.floor((time % 3600) / 60);
                    var secs  = Math.floor(time % 60);
     
                    if (secs < 10) {
                        secs = "0" + secs;
                    }
     
                    if (hours) {
                        if (mins < 10) {
                            mins = "0" + mins;
                        }
     
                        return hours + ":" + mins + ":" + secs; // hh:mm:ss
                    } else {
                        return mins + ":" + secs; // mm:ss
                    }
                }
     
                function clickProgress(idPlayer, control, event) {
                    var parent = getPosition(control);    // La position absolue de la progressBar
                    var target = getMousePosition(event); // L'endroit du la progressBar où on a cliqué
                    var player = document.querySelector('#' + idPlayer);
     
                    var x = target.x - parent.x;
                    var y = target.y - parent.y;
     
                    var wrapperWidth = document.querySelector('#progressBarControl').offsetWidth;
     
                    var percent  = Math.ceil((x / wrapperWidth) * 100);
                    var duration = player.duration;
     
                    player.currentTime = (duration * percent) / 100;
                }
     
                function getMousePosition(event) {
                    if (event.pageX) {
                        return {
                            x: event.pageX,
                            y: event.pageY
                        };
                    } else {
                        return {
                            x: event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
                            y: event.clientY + document.body.scrollTop  + document.documentElement.scrollTop
                        };
                    }
                }
     
                function getPosition(element){
                    var top = 0, left = 0;
     
                    while (element) {
                        left   += element.offsetLeft;
                        top    += element.offsetTop;
                        element = element.offsetParent;
                    }
     
                    return { x: left, y: top };
                }
    			</script>
            <style>
                /* syllabes */
                .syllabe
                {
                    color: red;
                    font-size: 16px;
                }
     
                .syllabehaute
                {
                    color: blue;
                    font-size: 20px;
                }
     
                .syllabebasse
                {
                    color: #FFA500;
                    font-size: 10px;
                }
     
     
                /* lecteur audio */
                #divPlayer
                {
                    width: 300px;
                    height: 120px;
                    margin-left: auto;
                    margin-right: auto;
                    border: 1px double #aaa;
                    border-radius: 2px;
                    box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
                    padding: 20px;
                    margin-top: 50px;
                }
     
                /*Titre*/
                h1
                {
                    font-size: 17px;
                    line-height: 30px;
                    margin: 0;
                    padding: 0;
                    margin-bottom: 25px;
                    border-bottom: #000 1px solid;
                }
     
                /*Bouton*/
                button, input[type='button'], input[type='submit']
                {
                    border-radius: 2px;
                    box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
                    background: -webkit-linear-gradient(#fafafa, #f4f4f4 40%, #e5e5e5);
                    background: -moz-linear-gradient(#fafafa, #f4f4f4 40%, #e5e5e5);
                    border: 1px solid #aaa;
                    color: #444;
                    margin-bottom: 0px;
                    min-width: 4em;
                    padding: 3px 12px 3px 12px;
                    font-size: 12px;
                    cursor: pointer;
                    display: inline-block;
                }
     
                button:hover, input[type='button']:hover, input[type='submit']:hover
                {
                    -webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.2);
                    background: #ebebeb -webkit-linear-gradient(#fefefe, #f8f8f8 40%, #e9e9e9);
                    background: #ebebeb -moz-linear-gradient(#fefefe, #f8f8f8 40%, #e9e9e9);
                    border-color: #999;
                    color: #222;
                    display: inline-block;
                }
     
                .control
                {
                    width: 78px;
                }
     
                /*barre de progression*/
                #progressBarControl
                {
                    width: 210px;
                    height: 15px;
                    border: 1px solid #aaa;
                    border-radius: 2px;
                    box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
                    margin-bottom: 5px;
                    display: inline-block;
                }
     
                #progressBar
                {
                    width: 0px;
                    background: #ebebeb;
                    height: 15px;
                    text-align: right;
                    line-height: 15px;
                    font-size: 9px;
                    white-space: nowrap;
                    cursor: pointer;
                }
     
                #progressTime
                {
                    font-size: 10px;
                    color: #666;
                }
     
                /*volume*/
                .volume
                {
                    vertical-align: bottom;
                    display: inline-block;
                    height: 20px;
                    white-space: nowrap;
                    position: relative;
                    width: 40px;
    				margin-left : 100px;
    				margin-bottom : 0px;
                }
     
                .volume a 
                {
                    display: inline-block;
                    background: #ebebeb;
                    width: 8px;
                    vertical-align: bottom;
                    margin-left: 165px;
                    margin-right: 0px;
                    margin-bottom: 70px;
                    cursor: pointer;
                    border-left: 1px solid #fff;
                    position: absolute;
                    bottom: 0;
     
                }
     
                .stick1 
                { 
                    height: 5px;  
                    left: 0px;
                }
                .stick2 
                { 
                    height: 10px; 
                    left: 9px;
                }
                .stick3 
                { 
                    height: 15px; 
                    left: 18px; 
                }
                .stick4 
                { 
                    height: 20px; 
                    left: 27px; 
                }
                .stick5 
                { 
                    height: 25px; 
                    left: 36px; 
                }
     
                /*Texte*/
                #divText
                {
                    width: 300px;
                    margin-left: auto;
                    margin-right: auto;
                    border: 1px double #aaa;
                    border-radius: 5px;
                    box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
                    padding: 20px;
                    margin-top: -20px;
                }
     
    			</style>
    	</head>
     
     
    	<body>
    		<div id="divText">
     
                <span class=text id="div1">
                    <span class="none">At</span> <span class="none">s</span><span class="none">ch</span><span class="syllabehaute">oo</span><span class="syllabehaute">l</span><span class="syllabehaute"></span> <span class="none">i</span> <span class="none">h</span><span class="syllabehaute">a</span><span class="syllabehaute">t</span><span class="normal">e</span><span class="normal">d</span>  <span class="syllabehaute">sci</span><span class="syllabebasse">e</span><span class="normal">n</span><span class="normal">c</span><span class="normal">e</span><span class="normal">s</span><span class="normal"></span> <span class="syllabehaute">sub</span><span class="none">ject</span><br>
                </span>
     
                <span class=text id="div2">
                    <span class="none">like</span> <span class="none">ph</span><span class="syllabehaute">y</span><span class="syllabehaute">s</span><span class="normal">i</span><span class="normal">cs</span><span class="normal">
                        <span class="none">ch</span><span class="syllabehaute">e</span><span class="syllabehaute">m</span><span class="normal">i</span><span class="normal">s</span><span class="normal">t</span><span class="normal">r</span><span class="normal">y</span>
                    </span> <span class="none">and</span>  <span class="none">bi</span><span class="syllabehaute">ol</span><span class="syllabebasse">o</span><span class="none">gy.</span><br>
                </span>
                <span class=text id="div3">
                    <span class="none">You</span> <span class="none">know,</span> <span class="none">and</span> <span class="none">…..</span> <span class="normal">I</span> <span class="normal">wasn’t</span>  <span class="normal">
                        <span class="none">very</span> <span class="none">good</span>
                    </span>
                    <span class=text id="div4">
                        <span class="syllabe">m</span><span class=" syllabe ">a</span><span class="syllabe">th</span><span class="syllabebasse">e</span><span class=" syllabehaute ">m</span><span class="syllabehaute">a</span><span class="syllabehaute">t</span><span class="normal">i</span><span class="normal">cs</span><span class="normal"></span> <span class="none">and</span> <span class="none">th</span><span class="syllabehaute">i</span><span class="syllabehaute">ng</span><span class="syllabehaute">s</span><span class="syllabehaute"></span>.
                        <br>
                    </span>
                    <span class=text id="div5">
                        <span class="none">I</span> <span class=" syllabehaute ">r</span><span class="syllabehaute">ea</span><span class="normal">ll</span><span class="normal">y</span>  <span class="none">liked</span> <span class="syllabehaute">sub</span><span class="none">ject</span> <span class="none">like</span>
                        <br>
                    </span>
     
                    <span class=text id="div6">
                        <span class=" syllabehaute ">h</span><span class=" syllabehaute ">i</span><span class="syllabehaute">s</span><span class="normal">t</span><span class="syllabefaible">o</span><span class="normal">ry</span>,
                        <span class="none">g</span><span class="normal">e</span><span class="syllabehaute">o</span><span class="syllabehaute">g</span><span class="NORMAL">r</span><span class="syllabebasse">a</span><span class="normal">ph</span><span class="normal">y</span><span class="normal"></span>,  <span class="syllabe">e</span><span class="syllabe">c</span><span class="syllabebasse">o</span><span class="normal">n</span><span class="syllabehaute">o</span><span class="syllabehaute">m</span><span class="normal">i</span><span class="normal">cs.</span><br>
                    </span>
     
                    <span class=text id="div7">
                    <span class="syllabehaute">A</span><span class="syllabehaute">n</span><span class="normal">y</span><span class="normal">w</span><span class="syllabe">ay</span><span class="syllabe"></span> <span class="none">when</span> <span class="none">i</span> <span class="none">went</span> <span class="none">to</span>  <span class="none">u</span><span class="none">ni</span><span class=" syllabehaute ">v</span><span class=" syllabehaute ">er</span><span class=" normal ">si</span><span class=" nomal ">ty</span><br>
                </span>
     
                    <span class=text id="div8">
                        <span class="none">I</span>  <span class="none">w</span><span class="syllabehaute">a</span><span class="syllabehaute">n</span><span class="syllabehaute">t</span><span class="normal">e</span><span class="normal">d</span> <span class="none">to</span> <span class="none">do</span> <span class="none">g</span><span class="normal">e</span><span class="syllabehaute">o</span><span class="syllabehaute">l</span><span class="syllabebasse">o</span><span class="normal">g</span><span class="normal">y</span><span class="normal"></span> <span class="none">but</span>  <span class="none">I</span> <span class="none">c</span><span class="syllabehaute">ou</span><span class="syllabehaute">l</span><span class="syllabehaute">d</span><span class="normal">n</span><span class="normal">'t</span><span class="normal"></span><br>
                    </span>
     
                    <span class=text id="div9">
                         <span class="none">b</span><span class="normal">e</span><span class="normal">c</span><span class="syllabehaute">au</span><span class="syllabehaute">s</span><span class="syllabehaute">e</span> <span class="none">I</span> <span class="none">was</span> <span class="none">no</span> <span class="none">good</span> <span class="none">at</span> <span class="syllabehaute">sci</span><span class="syllabebasse">e</span><span class="normal">n</span><span class="normal">c</span><span class="normal">e</span><span class="normal">s</span><span class="normal"></span>.<br>
                    </span>
                    <span class=text id="div10">
                        <span class="none">So</span> <span class="none">in</span> <span class="none">the</span> <span class="none">end</span> <span class="none">I</span> <span class="none">did</span> <span class="none">ph</span><span class="normal">il</span><span class="syllabehaute">o</span><span class="syllabehaute">s</span><span class="syllabebasse">o</span><span class="normal">ph</span><span class="normal">y</span><span class="normal"></span>.
                    </span>
     
     
     
     
     
    		</div>
    	</body>
     
    </html>

  2. #2
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    17 126
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 17 126
    Points : 44 922
    Points
    44 922
    Par défaut
    Bonsoir,
    pour le principe
    - tu récupères le innerHTML de la DIV
    - tu le stocke dans ta DIV
    - tu récupères le textContent|| innerText de la DIV
    - ensuite tu fais joujou avec l'un ou l'autre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    var oDiv= document.getElementById('divText');
    // Recup le contenu avec balisage
    var oInner = oDiv.innerHTML;
    // sauvegarde le innerHTML
    oDiv.oldInner = oInner;
     
    function showTextContent(){
      // Recup le contenu text only
      oDiv.innerHTML = oDiv.textContent|| oDiv.innerText;
    }
     
    function showinnerHTML(){
      oDiv.innerHTML = oDiv.oldInner;
    }
    on ne doit pas être loin de la solution.

  3. #3
    Membre chevronné

    Avatar de nicosmash
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2012
    Messages
    395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Vaucluse (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2012
    Messages : 395
    Points : 2 107
    Points
    2 107
    Par défaut
    Bonsoir,

    Vous pouvez faire quelque chose comme ceci aussi :

    Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    function setStyle(numstyle)
    {
    	if(numstyle==1)
    	{
    	document.getElementById('id_theme').className = "theme_1";
    	}
    	else if(numstyle==2)
    	{
    	document.getElementById('c_theme').className = "theme-2";
    }
    }

    En fonction du bouton sur lequel il clic il aura soit le CSS du theme_1 soit du theme_2.

Discussions similaires

  1. [XL-2010] VBA : Bouton contrôle de formulaire avec insertion forme et suppression ancienne
    Par anonymous9 dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 05/03/2014, 20h24
  2. Contrôle d'une animation avec des boutons
    Par nwktotof dans le forum Windows Presentation Foundation
    Réponses: 0
    Dernier message: 20/08/2009, 09h27
  3. Réponses: 5
    Dernier message: 23/10/2008, 10h36
  4. [VB.NET] Pb avec le bouton Annuler d'1 barre de progression
    Par dada1982 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 30/06/2004, 09h56
  5. Réponses: 10
    Dernier message: 10/06/2004, 16h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo