JQuery:找到父母和另一个孩子

时间:2012-05-15 09:33:15

标签: jquery

我有以下标记:

<div style="width: 660px; border-bottom: 1px solid #3A3F46;">
            <div class="noplace_event_cont">
                <div style="float: left;">
                    <span class="title">Usual</span> 
                    <span class="desc">Free: <span class="free_place">19</span></span>
                </div>
                <div style="float: right;">
                    <div class="spinner_cont">
                        <input type="text" data-type="1" class="event_spinner" value="0" maxlength="1" style="width: 24px; margin-right: 16px; text-align: right;"><span class="ui-spinner ui-widget"><div class="ui-spinner-buttons" style="height: 41px; left: -16px; top: -13.0833px; width: 36px;"><div class="ui-spinner-up ui-spinner-button ui-state-default ui-corner-tr" style="width: 16px; height: 20.5px;"><span class="ui-icon ui-icon-triangle-1-n" style="margin-left: 16.5px; margin-top: 15px;">&nbsp;</span></div><div class="ui-spinner-down ui-spinner-button ui-state-default ui-corner-br" style="width: 16px; height: 20.5px;"><span class="ui-icon ui-icon-triangle-1-s" style="margin-left: 16.5px; margin-top: 15px;">&nbsp;</span></div></div></span>
                    </div>
                </div>

            </div>
            <div class="noplace_event_cont">
                <div style="float: left;">
                    <span class="title">VIP</span> 
                    <span class="desc">Free: <span class="free_place">6</span></span>
                </div>
                <div style="float: right;">
                    <div class="spinner_cont">
                        <input type="text" data-type="2" class="event_spinner" value="0" maxlength="1" style="width: 24px; margin-right: 16px; text-align: right;"><span class="ui-spinner ui-widget"><div class="ui-spinner-buttons" style="height: 41px; left: -16px; top: -13.0833px; width: 36px;"><div class="ui-spinner-up ui-spinner-button ui-state-default ui-corner-tr" style="width: 16px; height: 20.5px;"><span class="ui-icon ui-icon-triangle-1-n" style="margin-left: 16.5px; margin-top: 15px;">&nbsp;</span></div><div class="ui-spinner-down ui-spinner-button ui-state-default ui-corner-br" style="width: 16px; height: 20.5px;"><span class="ui-icon ui-icon-triangle-1-s" style="margin-left: 16.5px; margin-top: 15px;">&nbsp;</span></div></div></span>
                    </div>
                </div>

            </div>
    </div>

我想在span中找到free_place类:

的文字
$(".event_spinner").each(function () {
            var max = $(this).parent(".noplace_event_cont").find(".free_place").text();
            alert(max);
        });

但是,这段代码不起作用。 max未定义。
感谢。

4 个答案:

答案 0 :(得分:2)

  

我想在free_place类中查找文本:

$('span .free_place').text()

或者这个:

$('span.free_place').text()

很难理解你想要输出的是什么。

答案 1 :(得分:1)

    $(".event_spinner").each(function () {
        var max = $(this).parents(".noplace_event_cont").find(".free_place").text();
        alert(max);
    });

答案 2 :(得分:1)

var max = $(this).closest(".noplace_event_cont").find(".free_place").text();

var max = $(this).closest(".free_place").text();

答案 3 :(得分:1)

使用.closest()

demo jsFiddle

$(".event_spinner").each(function () {
            var max = $(this).closest(".noplace_event_cont").find(".free_place").text();
            alert(max);
});