我误解了一个jQuery数组

时间:2015-08-20 16:40:37

标签: javascript jquery

此代码无效

var next = $("#orders").find(".next");
            if (next.length == 1) {
                var address = $(next[0]).find(".directionsAddress");
                var destination = $(address[0]).text();
            }

假设找到一个带有" next"的等级的div。我知道它存在于页面上,然后在结果集数组的那一项中,将有一个div,其类名为directionsAddress。 "下一个"数组以1的长度返回,所以看起来问题出在我的$(next [0])。找到因为地址数组返回0长度而我正在制作某种类型的语法错误不明白。

1 个答案:

答案 0 :(得分:0)

你打算做这样的事吗?



$(document).ready(function() {
	alert($('#orders div.next:first div.directionsAddress:first').text());
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "orders">
    <div class="next">
        <div class="directionsAddress">THIS IS WHAT I WANT</div>
        <div class="directionsAddress">This is not the one I want</div>
    </div>
    <div class="next">
        <div class="directionsAddress">This is not the one I want</div>
    </div>
    <div class="next">
        <div class="directionsAddress">This is not the one I want</div>
    </div>
</div>
&#13;
&#13;
&#13;