jQuery equivalent of Ruby any?
So i'm looking for a concise开发者_运维问答 way to determine if an array of objects returned from a selector have text.
My setup here is pretty basic, I've got a table and I'd like to determine if in a specified column I actually have data. I initially thought the .is() method would be my answer, but I just couldn't get it to return anything but false:
$('.draft-date').is(function() { return ($(this).text() === ""); }); // <-- return false
$('.draft-date').is(function() { return ($(this).text() != ""); }); // <-- return false, but based on test data should return true
Now, am I misunderstanding the .is() method? Is my code broken?
I've got a work around using .map and .inArray():
$.inArray(true, $.map($('.draft-date'), function(n, i) { return ($(n).text() != "");} ))
But I honestly don't like it much. It's fugly.
Help me beautify my garden StackOverflow.
Well, the simplest solution is to use the text
method on the whole selection, because that returns all the elements' text data.
if ($('.draft-date').text() === '') {
// all elements have no text
}
If any elements have text, that text will be returned.
This is because is
will only work on the first element in a set of elements. Otherwise, how would it know which result you wanted?
精彩评论