如果您的选择器对象无效,为什么jQuery不会弹出? [英] Why doesn't jQuery bomb if your selector object is invalid?

查看:141
本文介绍了如果您的选择器对象无效,为什么jQuery不会弹出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近在

  $(#divMenuContainer:visible)中使用了一些代码hide(explode ); 

然而,在一段时间内花费了很多时间,我意识到我的选择器引用了一个没有存在。



查询的结果只是它没有执行。



显然这是设计的,任何人都可以解释为什么选择这种设计的逻辑,而不是提出某种异常?



不要批评只是想了解。

解决方案

这里有一些很好的理由,可链接性是主要的驱动器,通过链接编写非常简洁的代码的能力不得不抛出任何错误,工作无理,例如:

  $( #divMenuContainer:可见)隐藏( 爆炸)添加( #另一个)淡入(); 

链中的每个对象即使没有引用DOM元素也可能稍后添加,或者让我们另一个例子:

  $(#divMenuContainer:visible)。live(click,function() 。}); 

在这种情况下,我们不关心选择器发现的任何元素,我们关心选择器本身这是另一个:

  $(#divMenuContainer:visible)。find(。child)hide(explode 。).END()淡出(); 

即使没有孩子,我们也可能希望再次回到连锁店,继续使用



有几十种不同的案例,显示了图书馆的好处是它的方式。对于为什么,来自 John Resig 的访谈,谁是jQuery的创建者,他说明这是怎么解决的。他的代码很简单,因为他可以得到它,链接模式是从帽子出来,它恰好也有很多好处,上面的例子只是其中的一些。



要清楚,我不是说链接的每一个属性是一个好的,只有很多事情。






我们以这个页面为例,如果我们有这样的话:

  $单击(replyToFunction)( 注释)。; 

应该失败,因为还没有任何评论?好吧,不是真的,这是预期的,我不想要一个错误在这里...如果元素存在做,如果不是没有。我的观点是,至少在我的经验中,因为缺少元素而引发错误比投掷一个元素更为有用。



您的问题中的选择器, #ID selector 是一个非常特殊的情况,您只希望只有一个元素,所以也许你可能会认为它应该在那里失败...但是这与其他选择器不一致,并且您希望库是一致的。



几乎任何其他选择器你希望有0个元素,所以当你没有找到任何元素的时候失败,在大多数情况下会更不理想,在这样的情况下更是如此,例如 .live() 以上。


Was recently using some code along the lines of

$("#divMenuContainer:visible").hide("explode");

However after some time spent trying to get it to work I realized my selector was referencing a div that didnt exist.

The result of the query was simply that it didn’t execute.

Obviously this is by design, could anyone explain the logic of why this design choice was made rather than raise some sort of exception?

Not trying to criticise just trying to understand.

解决方案

There are a few good reasons here, "chainability" is the main drive, the ability to write very terse code by chaining has to throw no errors to work seemlessly, for example:

$("#divMenuContainer:visible").hide("explode").add("#another").fadeIn();

Each object in the chain, even if it references no DOM elements may have more added later, or let's take another example:

$("#divMenuContainer:visible").live("click", function() { ... });

In this case we don't care about any of the elements the selector found, we care about the selector itself. Here's another:

$("#divMenuContainer:visible").find(".child").hide("explode").end().fadeOut();

Even if there are no children, we may want to hop back in the chain afterwards, continuing to use the .prevObject reference to go back up the chain.

There are dozens of distinct cases like this that show the benefits of the library being the way it is. As for the why, from interviews of John Resig, who is the creator of jQuery, he states that's just how it worked out. He was after code as terse as he could get it, and the chaining model is what came out of hat, it just happens to have a lot of benefits as well, the example above are just a few of those.

To be clear, I'm not saying every attribute of chaining is a good one, there are just many upsides to it.


Let's take this page as an example, what if we had something like this:

$(".comment").click(replyToFunction);

Should that fail because there aren't any comments yet? Well no not really, that's expected, I wouldn't want an error here...if the element exists do it, if not don't. My point is, at least in my experience, not throwing an error because of a missing element is tremendously more useful than throwing one.

The selector in your question, the #IDselector is a very special case where you expect only a single element, so maybe you could argue it should fail there...but then that wouldn't be consistent with other selectors, and you want a library to be consistent.

With pretty much any other selector you expect 0-many elements, so failing when you don't find any elements would be significantly less desirable in most situations, even more so in the cases like .live() above.

这篇关于如果您的选择器对象无效,为什么jQuery不会弹出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆