如何从jquery中的div中的相同span类获取值

时间:2014-07-22 09:13:19

标签: javascript jquery

我正试图从span获得电话号码011 92100355和011 37653145以及来自h4标签的地址,即AB 14,Harua chor Market,New Pndichery India

<div class="grid_16 bbd pb5 pt5">
    <div class="grid_16 column alpha phone-details ">
        <div id="resinfo-phone" class="p-relative">
            <div class="bold ttupper res-main-label column alpha">Phone:</div>
            <div class="grid_14 column omega res-main-phone">
                <div class="phone left" id="phoneNoString">
                    <div class="column alpha omega">
                        <span class="telephone">
                            <span class="tel">011 92100355</span>,
                            <span class="telephone">011 37653145</span>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="grid_16">
        <div class="res-main-address clearfix">
            <div class="bold res-main-label column alpha ttupper">Address:</div>
            <div class="grid_14 column omega">
                  <h4 class="res-main-address-text left">
 AB 14, <strong itemprop="addressLocality">Harua</strong> Chor Market, New Pondichery
 <span class="hidden" itemprop="addressCountry">India</span></h4>

            </div>
        </div>
    </div>
</div>
</div>
</div>

我已经尝试过这段代码来获取地址和电话号码,但它无效。

$(document).ready(function() {
    $('.grid_16.bbd.pb5.pt5').find('span').each(function(){
        alert($(this).text());
    });
});

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

试试这个

  $(document).ready(function() {
     var tel = [];

        $(".telephone span").each(function(i,v) {

            tel[i] = $.trim($(this).text());
        });

        var address =$("h4.res-main-address-text").text();

        console.log(tel);
        console.log(address);
    });

Working DEMO

答案 1 :(得分:0)

 $(".grid_16").find("h4").text(); //address
  $(".grid_16").find("span").text();// phone no

DEMO