jQuery:如果在链内?
我有这个
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有像您想要的那样记录,但也许这对您有用:
为什么是 400,因为 fadeOut 的默认持续时间是 400 毫秒。 (来自文档)
There is nothing documented like you want but maybe this will work for you:
Why 400, because the default duration for fadeOut is 400 milliseconds. (from documentation)
您可以使用 $.fn.each 来做到这一点:
但是它比简单地做您已经在做的事情效率低得多。
编辑:这是另一种方法:
基本上,如果 noDelay 为 true,它将在删除之前分离元素,否则,它将在删除之前淡出它们。应该和你的代码一样高效,只需 1 行。
You could do this with a $.fn.each:
however it is far less efficient than simply doing what you already are doing.
Edit: here's another way to do it:
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.
对于这样的事情有异议吗?
Any objection to something like this?
或者你可以使用一个可怕的黑客:
Or you could use a horrible hack:
这个特殊问题:
尝试稍微重新思考逻辑,并使
noDelay
真正影响延迟。虽然我不确定
remove()
是否有必要。当我对另一个问题进行
fadeOut()
测试时,它似乎隐藏并折叠了元素。remove()
会从 DOM 中完全删除该元素,但我不确定如果你只是想让它从文档中消失并让它停止影响文档流(没有间隙,是的)。真正的目标:
此外,看起来您正计划包装 jQuery。您最终将包装这样的代码:
...并将其更改为类似以下内容:
...或:
除非您计划大量重用该特定元素,否则我认为您会浪费时间。 jQuery 对于一次性操作具有非常高效的语法,并且您始终可以将匹配的元素存储在临时变量中。
如果您还有我所遗漏的其他目的,请原谅对您设计的干扰:)
This particular problem:
Try rethinking the logic slightly, and make the
noDelay
actually effect the delay.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:
...and changing it to something like:
...or:
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 :)