试图在javascript中选择nav ul li li li

时间:2014-08-13 21:18:33

标签: javascript html css

我尝试过很多选项,但是到目前为止我还没有这个选项。

document.querySelectorAll('ul li').querySelectorAll('ul li').style.background = color;

document.getElementsByTagName("NAV").getElementsByTagName("UL").getElementsByTagName("LI").getElementsByTagName("UL").getElementsByTagName("LI").style.background = color;

我的HTML

<nav>
    <ul id = "mainNav">
        <li><center>
            <img id = "logo" src = "https://31.media.tumblr.com/cfc2c43f7e34ec3fcb60405fa5d4f5a5/tumblr_n9nvpjtNNW1tgkx81o1_1280.png"></center>
        </li>
        <li id = "search">

            <form name = "searchField">
                <img src = "http://31.media.tumblr.com/7732f8901e9c94cb94aa5cff9e11b2ae/tumblr_n8zm5cD1SL1tgkx81o1_1280.png"><input name = "search" type = "text" placeHolder = "" id = "search">
            </form>

        </li>
        <li><a href ="#">
            <img src = "https://38.media.tumblr.com/f319cbdb3a9bd4041fd2c6056827794b/tumblr_n9lpsfYFOB1tgkx81o3_1280.png" alt = "img/home.png">HOME</a>
        </li>
        <li><a href ="#">
            <img src = "https://31.media.tumblr.com/1dc400960f59fca2f738ceab516d24f4/tumblr_n9lpsfYFOB1tgkx81o4_1280.png" alt = "img/info.png">ABOUT</a>
        </li>
        <li id = "section"><a href ="#">
            <img src = "https://38.media.tumblr.com/11cb610a9c031eaf5bb8789cb9739717/tumblr_n9lpsfYFOB1tgkx81o1_1280.png"alt = "img/camera.png">PHOTOGRAPHY</a>
            <ul>
                <li><a href = "/photography">San Francisco</a></li>
                <li><a href = "#">Lake Tahoe</a></li>
            </ul>
        </li>
        <li id = "section"><a href ="#">
            <img src = "https://31.media.tumblr.com/bd112082d5c902750e78aa9d7dd817fc/tumblr_n9lpsfYFOB1tgkx81o5_1280.png"alt = "img/projects.png">PROJECTS</a>
            <ul>
                <li><a href = "/projects#informative">Informative</a></li>
                <li><a href = "/projects#profile">Profile</a></li>
            </ul>
        </li>
        <li id = "section"><a href ="#">
            <img src = "https://33.media.tumblr.com/e9a7f9ae946bdf2950be96f27023bb78/tumblr_n9lpsfYFOB1tgkx81o2_1280.png"alt = "img/contact.png">CONTACT</a>

            <ul>
                <li><a href = "http://www.twitter.com/skarchmit" target =" _blank">Twitter</a></li>
                <li><a href = "http://www.instagram.com/skarchmit" target =" _blank">Instagram</a></li>
                <li><a href="mailto:skarchit@gmail.com" target="_top">Email</a></li>
            </ul>
        </li>
    </ul>
</nav>
            <nav>
                <ul id = "viewNav">
                    <a href = "#"><li> Some things here</li></a>
                </ul>
            </nav>

有没有人知道如何使用javascript选择nav ul li li li?

P.S。我还没学过jquery。所以我想用纯粹的javascript来做。

4 个答案:

答案 0 :(得分:3)

“如何使用javascript选择nav ul li ul li”

你是说这个吗?

document.querySelectorAll('nav > ul > li > ul > li');

请注意,您可以删除适合您的直接子条件>

另请注意,querySelectorAll会返回一个集合(非实时NodeList),因此您必须对其进行迭代才能操作每个元素。

   var nestedLis = document.querySelectorAll('nav > ul > li > ul > li'),
       i = 0,
       len = nestedLis.length;

   for (; i < len; i++) nestedLis[i].style.backgroundColor = 'red';

由于大多数数组函数都可以操作类似数组的对象,因此您也可以将[].forEach应用于节点列表。

[].forEach.call(nestedLis, function (li) {
    //manipulate li
});

答案 1 :(得分:1)

QuerySelectorAll返回对象的NodeList,即使只有一个 所以你需要在NodeList中给出元素位置,如此

document.querySelectorAll('ul li')[0]

第一个元素。

如果你需要通过所有元素,你需要一个像这样的循环

var elems = document.querySelectorAll('ul li');

for (var i=0; i<elems.length; i++) {
    elems[i].style.background = color;
}

答案 2 :(得分:0)

这应该有效

document.querySelectorAll('NAV ul li ul li')

答案 3 :(得分:0)

只是为了包含jQuery解决方案..

$('nav ul li ul li')

或在子菜单中添加一个类,如#myID和target

$('#myID li')