Traitement toute html avant de se lier vol stationnaire et mettre quelques travées etc. est ok
Vous auriez certainement pour ce faire, car vous ne pouvez pas styler des nœuds de texte, seulement des éléments.
Voici une fonction que vous pourriez utiliser pour le faire depuis un script. (Malheureusement, jQuery n'est pas très ici car il n'aime pas les nœuds de texte de manipulation.)
// Wrap Text nodes in a new element of given tagname, when their
// parents contain a mixture of text and element content. Ignore
// whitespace nodes.
//
function wrapMixedContentText(el, tag) {
var elementcontent= false;
for (var i= el.childNodes.length; i-->0;) {
var child= el.childNodes[i];
if (child.nodeType===1) {
elementcontent= true;
wrapMixedContentText(child, tag);
}
}
if (elementcontent) {
for (var i= el.childNodes.length; i-->0;) {
var child= el.childNodes[i];
if (child.nodeType===3 && !child.data.match('^\\s*$')) {
var wrap= document.createElement(tag);
el.replaceChild(wrap, child);
wrap.appendChild(child);
}
}
}
}
Et voici quelques fonctions que vous pouvez utiliser pour sélectionner des noeuds entre autres noeuds. (Encore une fois, jQuery n'a pas une fonction pour cela.)
// Get array of outermost elements that are, in document order,
// between the two argument nodes (exclusively).
//
function getElementsBetweenTree(start, end) {
var ancestor= getCommonAncestor(start, end);
var before= [];
while (start.parentNode!==ancestor) {
var el= start;
while (el.nextSibling)
before.push(el= el.nextSibling);
start= start.parentNode;
}
var after= [];
while (end.parentNode!==ancestor) {
var el= end;
while (el.previousSibling)
after.push(el= el.previousSibling);
end= end.parentNode;
}
after.reverse();
while ((start= start.nextSibling)!==end)
before.push(start);
return before.concat(after);
}
// Get the innermost element that is an ancestor of two nodes.
//
function getCommonAncestor(a, b) {
var parents= $(a).parents().andSelf();
while (b) {
var ix= parents.index(b);
if (ix!==-1)
return b;
b= b.parentNode;
}
return null;
}
utilisation possible:
var outer= document.getElementById('myhighlightingimagesdiv');
wrapMixedContentText(outer, 'span');
var ps= $('#myhighlightingimagesdiv .p');
ps.each(function(pi) {
// Go up to the next image in the list, or for the last image, up
// to the end of the outer wrapper div. (There must be a node
// after the div for this to work.)
//
var end= pi===ps.length-1? outer.nextSibling : ps[pi+1];
var tweens= $(getElementsBetweenTree(this, end));
$(this).hover(function() {
tweens.addClass('highlight');
}, function() {
tweens.removeClass('highlight');
});
});
C'est beaucoup de code intéressant. Je l'ai travaillé de façon à ce qu'il fonctionne assez vite pour réagir aux survols en ajoutant beaucoup de portées tout en générant la sortie html, mais je vais regarder tout votre code, parce que je veux toujours savoir comment puis-je obtenir ces éléments. Merci pour ce travail. Je vous donnerais plus de +1, mais il ne me laissera pas;) Je pense que je ferai un plugin si vous n'envisagez pas de le faire. – naugtur