最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select the first span element which is not empty - Stack Overflow

programmeradmin0浏览0评论

From a group of span elements, I want to get the values of the first occurrence of a span that is not empty.

I have this HTML

<span class="map_multi_latitude"></span>
<span class="map_multi_longitude"></span>
<span class="map_multi_latitude">30.201998</span>
<span class="map_multi_longitude">120.990876</span>

I have this Jquery before but I want to make it work such that "the first span whose text is not empty":

initialLat = $('span.map_multi_latitude:first').text();
initialLng = $('span.map_multi_longitude:first').text();

thanks in advance!

From a group of span elements, I want to get the values of the first occurrence of a span that is not empty.

I have this HTML

<span class="map_multi_latitude"></span>
<span class="map_multi_longitude"></span>
<span class="map_multi_latitude">30.201998</span>
<span class="map_multi_longitude">120.990876</span>

I have this Jquery before but I want to make it work such that "the first span whose text is not empty":

initialLat = $('span.map_multi_latitude:first').text();
initialLng = $('span.map_multi_longitude:first').text();

thanks in advance!

Share Improve this question edited Jun 15, 2011 at 3:37 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked Jun 15, 2011 at 3:26 yretutayretuta 8,09118 gold badges84 silver badges153 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 16

This will work:

$('span:not(:empty):first').text()

or these:

$('span:not(:empty):eq(0)').text()
$('span:not(:empty)').eq(0).text()
$('span:not(:empty)').slice(0,1).text()
$('span:not(:empty)').first().text()
initialLat = $('span.map_multi_latitude:not(:empty):first').text();
initialLng = $('span.map_multi_longitude:not(:empty):first').text();
var firstMapMultiLatitude;
$('.map_multi_latitude').each(function (index, element) {
    if ($(element).val() != '') {
        firstMapMultiLatitude = $(element).val();
        alert($(element).val());
        return;
    }
});
发布评论

评论列表(0)

  1. 暂无评论