开发者

Javascript get nextSibling not class "foo"

I need to get the next sibling of an element but it should not h开发者_StackOverflow中文版ave the class "foo". In jQuery this would be:

myEl.next("div.goodClass")
or
myEl.next().not(".foo")

Can this be done with nextSibling?


You can to check the className property of nextSibling using a regular expression:

if (/(?:^|\s)foo(?:\s|$)/.test(el.nextSibling.className)) {
    // next sibling has foo class
}

If you want to find the next element that doesn't have that class, you can do something like this:

var el = document.getElementById("myEl");

while (el = el.nextSibling) {
    if (!(/(?:^|\s)foo(?:\s|$)/.test(el.className)))
        break;
}

// el is either null, or an element that doesn't have the foo class


Try this:

if (elem.nextSibling && !/(?:^|\s+)foo(?:\s|$)/.test(elem.nextSibling.className)) {
    // there is a next sibling that does not have the class foo
}


Try this code,

x=myEl.nextSibling;
while (x.nodeType!=1)
{
    if(x.className=="foo"){
       x=x.nextSibling;
    }
}
return x;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜