为具有相同类的元素的子项绑定事件

时间:2012-08-16 04:10:20

标签: jquery

我想为特定类的所有秒元素绑定一个click事件,我正在使用;

$("ul.message-list-row > li:eq(1) > div").click(function(){...});
不幸的是,它不起作用。我也试过没有“>”但没有运气。

我的代码中有7个相同的UL。所以,

console.log($("ul.message-list-row").length);

返回7,但

console.log($("ul.message-list-row > li:eq(1)").length);

返回1.不应该是7吗?这是什么错误?

顺便说一句,我正在尝试使用以下标记;

<ul class="message-list-row rounded-corners">
    <li>
        <img src="Public/CSS/Images/sample-app-icon-02.png" alt="" title="" />
        <span class="app-name">AppName</span>
        <span class="version">V3.1</span>
        <div class="apple">OS V. - 5.04</div>
    </li>
    <li>
        <div>
            <span>Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Nulla vitae elit libero, a pharetra augue. Maecenas sed diam eget risus varius blandit sit amet non magna.Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Nulla vitae elit libero, a pharetra augue. Maecenas sed diam eget risus varius blandit sit amet non magna.</span>
        </div>
    </li>
    <li>
        <div class="progress-bar" title="70"></div>
        <span>%70</span>
    </li>
    <li class="number">7643</li>
    <li class="completed">&nbsp;</li>
    <li>&nbsp;</li>
    <li>
        <ul>
            <li>Language:</li>
            <li>
                <img src="Public/CSS/Images/flag-uk.png" alt="" title="" />
                English
            </li>

            <li>Operator:</li>
            <li>Operator</li>

            <li>Country:</li>
            <li>
                <img src="Public/CSS/Images/flag-tr.png" alt="" title="" />
                Country
            </li>

            <li>Badge:</li>
            <li>Clean</li>
        </ul>
    </li>
</ul>


<ul class="message-list-row rounded-corners">
    <li>
        <img src="Public/CSS/Images/sample-app-icon-03.png" alt="" title="" />
        <span class="app-name">AppName</span>
        <span class="version">V3.1</span>
        <div class="apple">OS V. - 5.04</div>
    </li>
    <li>
        <div>
            <span>Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Nulla vitae elit libero, a pharetra augue. Maecenas sed diam eget risus varius blandit sit amet non magna.Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Nulla vitae elit libero, a pharetra augue. Maecenas sed diam eget risus varius blandit sit amet non magna.</span>
        </div>
    <li>
        <div class="progress-bar" title="0"></div>
        <span>%0</span>
    </li>
    <li class="date">00:22:58</li>
    <li class="waiting">&nbsp;</li>
    <li>
        <a class="settings-button" href=""></a>
        <a class="approve-button action-button" href=""></a>
        <a class="cancel-button action-button" href=""></a>
    </li>
    <li>
        <ul>
            <li>Language:</li>
            <li>
                <img src="Public/CSS/Images/flag-uk.png" alt="" title="" />
                English
            </li>

            <li>Operator:</li>
            <li>Operator</li>

            <li>Country:</li>
            <li>
                <img src="Public/CSS/Images/flag-tr.png" alt="" title="" />
                Country
            </li>

            <li>Badge:</li>
            <li>Clean</li>
        </ul>
    </li>
</ul>

1 个答案:

答案 0 :(得分:1)

:eq()不起作用,因为您似乎认为它确实如此。使用:nth-child(2)代替:eq(1)

  

:nth-child选择器获取jQuery集中的每个元素,该元素是其父元素的第n个子元素。 :eq选择器只能在jQuery集中获得一个元素,即“nth”(0索引)元素。

http://jsfiddle.net/mattball/xhYaP/