代码之家  ›  专栏  ›  技术社区  ›  Mircea

Javascript get nextSibling不是类“foo”

  •  0
  • Mircea  · 技术社区  · 15 年前

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

    这能用吗 下一页

    3 回复  |  直到 15 年前
        1
  •  0
  •   Gumbo    15 年前

    试试这个:

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

    类名 下一页

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

    如果要查找下一个没有该类的元素,可以执行以下操作:

    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
    
        3
  •  0
  •   Chinmayee G    15 年前

    试试这个密码,

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