jQuery 相当于 Ruby 吗?

发布于 2024-12-09 06:57:34 字数 634 浏览 3 评论 0原文

因此,我正在寻找一种简洁的方法来确定从选择器返回的对象数组是否包含文本。

我这里的设置非常基本,我有一个表,我想确定在指定的列中我是否确实有数据。我最初认为 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-12-16 06:57:34

那么,最简​​单的解决方案是对整个选择使用 text 方法,因为这会返回所有元素的文本数据。

if ($('.draft-date').text() === '') {
    // all elements have no 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 ($('.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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文