Mettre des lignes de code JS et jQuery dans un fichier à part
Bonjour,
j'ai fait le code suivant, qui n'est pas achevé (voir ce post pour ceux que cela intéresse) :
Code:
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
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Sources:
http://www.developpez.net/forums/d629718/webmasters-developpement-web/javascript/bibliotheques-frameworks/jquery/changer-nom-class-clic-jquery/#post3711520
http://www.developpez.net/forums/d1001334/webmasters-developpement-web/javascript/bibliotheques-frameworks/jquery/parcourir-lignes-tableau-partir-ligne-cliquee/#post5600151
http://api.jquery.com/html/
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"/>
<script type="text/javascript">
function changeSymbolFold(oneLine, showLine){
oneLineHtmlContent = oneLine.html();
if(showLine){
oneLineHtmlContent = oneLineHtmlContent.replace('class="symbolFold"><pre> + </pre>', 'class="symbolFold"><pre> - </pre>');
}else{
oneLineHtmlContent = oneLineHtmlContent.replace('class="symbolFold"><pre> - </pre>', 'class="symbolFold"><pre> + </pre>');
}
oneLine.html(oneLineHtmlContent);
}
function hideOrShow(oneLine, showLine){
if(showLine){
oneLine.show();
}else{
oneLine.hide();
}
}
function changeClassFold(oneLine){
if(oneLine.attr("class") == 'fold_start_closed'){
showLine = true;
oneLine
.addClass("fold_start_opened")
.removeClass("fold_start_closed")
}else if(oneLine.attr("class") == 'fold_start_opened'){
showLine = false;
oneLine
.addClass("fold_start_closed")
.removeClass("fold_start_opened")
}
return showLine;
}
function goToTheEndOfTheBlock(oneLine, showLine){
hideOrShow(oneLine, showLine)
while(oneLine.attr("class") != "fold_end"){
oneLine = oneLine.next();
}
oneLine = oneLine.next();
if(oneLine.attr("class") == "fold_start_closed"){
oneLine = goToTheEndOfTheBlock(oneLine, showLine)
}
return oneLine;
}
$(document).ready(function(){
$('tr[class^="fold_start"]').click(function(){
goToNext = true;
nbOfBlocksMet = 0;
showLine = changeClassFold($(this));
currentLine = $(this);
changeSymbolFold(currentLine, showLine);
while(goToNext){
currentLine = currentLine.next();
if(currentLine.attr("class") == "fold_start_closed"){
currentLine = goToTheEndOfTheBlock(currentLine, showLine)
}
if(currentLine.attr("class") == "listing_end"){
if(nbOfBlocksMet != 0 && showLine == false){
showLine = true;
}
goToNext = false;
}else if(currentLine.attr("class") == "fold_end"){
if(nbOfBlocksMet == 0){
goToNext = false;
}
nbOfBlocksMet -= 1;
}else if(currentLine.attr("class") == "fold_start_opened"){
nbOfBlocksMet += 1;
}
hideOrShow(currentLine, showLine)
}
});
});
</script>
</head>
<body>
<div>
Un petit exemple de listing....
</div>
<code>
<table >
<tr class="fold_start_opened" style="cursor:pointer;">
<td><pre>01</pre></td>
<td class="symbolFold"><pre> - </pre></td>
<td><pre>def function(bidon):</pre></td>
</tr>
<tr class="">
<td><pre>02</pre></td>
<td></td>
<td><pre> """</pre></td>
</tr>
<tr class="">
<td><pre>03</pre></td>
<td></td>
<td><pre> Un petit commentaire...</pre></td>
</tr>
<tr class="">
<td><pre>04</pre></td>
<td></td>
<td><pre> """</pre></td>
</tr>
<tr class="">
<td><pre>05</pre></td>
<td></td>
<td></td>
</tr>
<tr class="">
<td><pre>06</pre></td>
<td></td>
<td><pre> print('Cette fonction est bidon...')</pre></td>
</tr>
<tr class="fold_start_opened" style="cursor:pointer;">
<td><pre>07</pre></td>
<td class="symbolFold"><pre> - </pre></td>
<td><pre> for i in range(5):</pre></td>
</tr>
<tr class="">
<td><pre>08</pre></td>
<td></td>
<td><pre> print(i)</pre></td>
</tr>
<tr class="fold_end">
<td><pre>09</pre></td>
<td></td>
<td><pre> print(i**2)</pre></td>
</tr>
<tr class="">
<td><pre>10</pre></td>
<td></td>
<td><pre># Une autre boucle...</pre></td>
</tr>
<tr class="fold_start_opened" style="cursor:pointer;">
<td><pre>11</pre></td>
<td class="symbolFold"><pre> - </pre></td>
<td><pre> for k in range(5):</pre></td>
</tr>
<tr class="fold_end">
<td><pre>12</pre></td>
<td></td>
<td><pre> print('='*k)</pre></td>
</tr>
<tr class="listing_end">
<td><pre>13</pre></td>
<td></td>
<td><pre># Fin du code...</pre></td>
</tr>
</table>
</code>
<div>
C'est fini !!!
</div>
</body>
</html> |
Je voudrais mettre les lignes de code dans un fichier JS à part qui pourrait ainsi être importé dans différentes pages HTML.
Que faut-il faire ? Y-a-t-il des pièges à éviter ?