From a group of span elements, I want to get the values of the first o开发者_高级运维ccurrence 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!
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;
}
});
精彩评论