jQuery:如果在链内?

发布于 2024-12-20 01:51:14 字数 420 浏览 3 评论 0原文

我有这个

if(noDelay){
  $(element).find("." + options.class).remove();
} else {
  $(element).find("." + options.class).fadeOut().remove();
}

有没有办法可以避免重复该句子并仅在满足给定条件时添加 fadeOut()

我无法将 fadeOut() 移动到链的末尾,这可能会让事情变得更容易。

我在想类似的事情,

$(element).find("." + options.class).(if(noDelay) fadeOut()).remove();

提前谢谢, 狮子座

I have this

if(noDelay){
  $(element).find("." + options.class).remove();
} else {
  $(element).find("." + options.class).fadeOut().remove();
}

Is there a way I could avoid repeating the sentence and only add the fadeOut() when a given condition is met?

I can't move fadeOut() till the end of the chain, which probably would've made things easier.

I'm thinking something like

$(element).find("." + options.class).(if(noDelay) fadeOut()).remove();

Thanks in advance,
Leo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

笑忘罢 2024-12-27 01:51:14

没有像您想要的那样记录,但也许这对您有用:

$(element).find("." + options.class).fadeOut(noDelay ? 0 : 400).remove();

为什么是 400,因为 fadeOut 的默认持续时间是 400 毫秒。 (来自文档

There is nothing documented like you want but maybe this will work for you:

$(element).find("." + options.class).fadeOut(noDelay ? 0 : 400).remove();

Why 400, because the default duration for fadeOut is 400 milliseconds. (from documentation)

依 靠 2024-12-27 01:51:14

您可以使用 $.fn.each 来做到这一点:

$(element).find("." + options.class).each(function(){
  if (nodelay) {
    $(this).remove();
  }
  else {
    $(this).fadeOut().remove();
  }
});

但是它比简单地做您已经在做的事情效率低得多。

编辑:这是另一种方法:

$(element).find("." + options.class)[  noDelay ? "detach" : "fadeOut"  ]().remove();

基本上,如果 noDelay 为 true,它将在删除之前分离元素,否则,它将在删除之前淡出它们。应该和你的代码一样高效,只需 1 行。

You could do this with a $.fn.each:

$(element).find("." + options.class).each(function(){
  if (nodelay) {
    $(this).remove();
  }
  else {
    $(this).fadeOut().remove();
  }
});

however it is far less efficient than simply doing what you already are doing.

Edit: here's another way to do it:

$(element).find("." + options.class)[  noDelay ? "detach" : "fadeOut"  ]().remove();

basically, if noDelay is true, it will detach the elements before removing, else, it will fade them out before removing. Should be just as efficient as your code, just on 1 line.

感情废物 2024-12-27 01:51:14

对于这样的事情有异议吗?

var $found = $(element).find("." + options.class);
if (noDelay){
  $found.remove();
} else {
  $found.fadeOut().remove();
}

Any objection to something like this?

var $found = $(element).find("." + options.class);
if (noDelay){
  $found.remove();
} else {
  $found.fadeOut().remove();
}
时间海 2024-12-27 01:51:14
var els = $(element).find("." + options.class);
if (!nodelay) els.fadeOut();
els.remove();

或者你可以使用一个可怕的黑客:

$(element).find(…)[ nodelay ? 'somenoopfunction' : 'fadeOut' ]().remove();
var els = $(element).find("." + options.class);
if (!nodelay) els.fadeOut();
els.remove();

Or you could use a horrible hack:

$(element).find(…)[ nodelay ? 'somenoopfunction' : 'fadeOut' ]().remove();

这个特殊问题:

尝试稍微重新思考逻辑,并使 noDelay 真正影响延迟

$(element).find("." + options.class).fadeOut(noDelay ? 0 : 'normal').remove();

虽然我不确定 remove() 是否有必要。

当我对另一个问题进行 fadeOut() 测试时,它似乎隐藏并折叠了元素。 remove() 会从 DOM 中完全删除该元素,但我不确定如果你只是想让它从文档中消失并让它停止影响文档流(没有间隙,是的)。

真正的目标:

此外,看起来您正计划包装 jQuery。您最终将包装这样的代码:

$("someElement").find(".someClass").fadeOut().remove();

...并将其更改为类似以下内容:

fadeOut("someElement", { "class" : "someClass" });

...或:

var element = new SomeClass("someElement");
element.options.class = "someClass";
element.fadeOut();

除非您计划大量重用该特定元素,否则我认为您会浪费时间。 jQuery 对于一次性操作具有非常高效的语法,并且您始终可以将匹配的元素存储在临时变量中。

如果您还有我所遗漏的其他目的,请原谅对您设计的干扰:)

This particular problem:

Try rethinking the logic slightly, and make the noDelay actually effect the delay.

$(element).find("." + options.class).fadeOut(noDelay ? 0 : 'normal').remove();

Though I'm not sure if remove() is necessary.

When I was doing fadeOut() tests for another question, it seemed to hide and collapse the element. remove() would completely remove the element from the DOM, but I'm not sure it is necessary if you just want to make it disappear from the document and have it stop effecting document flow (no gap where it was).

Real goal:

Also, it looks like you're planning to wrap jQuery. You'll end up wrapping code like this:

$("someElement").find(".someClass").fadeOut().remove();

...and changing it to something like:

fadeOut("someElement", { "class" : "someClass" });

...or:

var element = new SomeClass("someElement");
element.options.class = "someClass";
element.fadeOut();

Unless you're planning on reusing that particular element a lot, I think you're going to be wasting your time. jQuery has pretty efficient syntax for one-off operations, and you can always store matched elements in a temporary variable.

If you have some other purpose in mind that I'm missing, please excuse this intrusion on your design :)

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