jQuery 相当于 Ruby 吗?
因此,我正在寻找一种简洁的方法来确定从选择器返回的对象数组是否包含文本。
我这里的设置非常基本,我有一个表,我想确定在指定的列中我是否确实有数据。我最初认为 .is() 方法将是我的答案,但我只是无法让它返回除 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
现在,我是否误解了 .is() 方法?我的代码坏了吗?
我有一个使用 .map 和 .inArray() 的方法:
$.inArray(true, $.map($('.draft-date'), function(n, i) { return ($(n).text() != "");} ))
但老实说我不太喜欢它。真难看。
帮助我美化我的花园 StackOverflow。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,最简单的解决方案是对整个选择使用
text
方法,因为这会返回所有元素的文本数据。如果任何元素有文本,则将返回该文本。
这是因为is
仅适用于一组元素中的第一个元素。否则,它怎么知道你想要哪个结果?Well, the simplest solution is to use the
text
method on the whole selection, because that returns all the elements' text data.If any elements have text, that text will be returned.
This is becauseis
will only work on the first element in a set of elements. Otherwise, how would it know which result you wanted?