在绑定hover和放置一些跨距等之前处理整个html是可以的
当然,您必须这样做,因为您不能设置文本节点的样式,只能设置元素的样式。
这里有一个函数,你可以用它从脚本。(不幸的是,jquery在这里没有多大用处,因为它不喜欢处理文本节点。)
// 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);
}
}
}
}
这里有一些函数可以用来选择其他节点之间的节点。(同样,jquery目前没有此功能。)
// 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;
}
可能的用途:
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');
});
});