jquery选择两个不是兄弟元素的元素之间的元素

nau*_*tur 2 jquery select dom

(我删除了属性,但它是一些自动生成的HTML.)

<img class="p"/>
<div> hello world
    <p>
        <font><font size="2">text.<img class="p"/>
        some text
        </font></font>
    </p>
    <img class="p"/>
    <p> <font><font size="2">more text<img class="p"/>
        another piece of text
        </font></font>
    </p><img class="p"/> some text on the end
</div>
Run Code Online (Sandbox Code Playgroud)

我需要img.p在悬停第一个元素时,对两个最接近(在HTML代码中)元素之间的所有文本应用一些带背景的突出显示.我不知道该怎么做.让我们说我徘徊在第一个img.p- 它应该突出显示hello world,text.而不是别的.

而现在最糟糕的部分 - 我需要背景在mouseleave上消失.

我需要它来处理任何可能的HTML混乱.以上只是一个例子,文件的结构会有所不同.

提示:只要不改变输出文档的外观,就可以在绑定悬停和放置一些跨度等之前处理整个html.

bob*_*nce 5

在绑定悬停和放置一些跨度等之前处理整个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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里有一些函数可用于在其他节点之间选择节点.(同样,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;
}
Run Code Online (Sandbox Code Playgroud)

可能的用法:

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');
    });
});
Run Code Online (Sandbox Code Playgroud)