简体   繁体   English

我是否真的需要检查jQuery是否存在某个元素?

[英]Do I really need to check if an element exists with jQuery?

I recently had a question about how to correctly check if an element exists with jQuery. 我最近有一个关于如何正确检查jQuery是否存在元素的问题。 I found the answer from here: 我从这里找到答案:

https://learn.jquery.com/using-jquery-core/faq/how-do-i-test-whether-an-element-exists/ https://learn.jquery.com/using-jquery-core/faq/how-do-i-test-whether-an-element-exists/

In summary: 综上所述:

if ( $( "#myDiv" ).length ) {
    // Do something
}

One guy I work with says the correct way to check should be: 与我一起工作的一个人说正确的检查方法应该是:

if ($( "#myDiv" ) && $( "#myDiv" ).length ) {
    // Do something
}

Does it matter? 有关系吗? I mean from a performance or latency wise, do they perform the same? 我的意思是从性能或延迟角度来看,它们是否表现相同?

Also: 也:

$( "#myDiv" ).show();
$( "#myDiv" ).hide();
$( "#myDiv" ).val('');

In these type of jQuery functions, it seems no if check is needed because they won't raise an error if #myDiv does not exist, correct? 在这些类型的jQuery的功能,似乎没有if ,因为他们不会引发错误,如果需要检查#myDiv不存在,是否正确?

For what it's worth, I am using jQuery 1.6.4. 对于它的价值,我正在使用jQuery 1.6.4。

One guy I work with says the correct way to check should be: 与我一起工作的一个人说正确的检查方法应该是:

 if ($( "#myDiv" ) && $( "#myDiv" ).length ) { //do something } 

He's wrong, at least in terms of the $( "#myDiv" ) && part. 他是错误的,至少就$( "#myDiv" ) &&部分而言。 jQuery's $(selector) always returns an object, which by definition is truthy. jQuery的$(selector) 总是返回一个对象,根据定义,该对象是真实的。 So that part is pointless, and re-querying the DOM for no reason. 因此,该部分毫无意义,并且无缘无故地重新查询DOM。

I mean from a performance or latency wise, do they perform the same? 我的意思是从性能或延迟角度来看,它们是否表现相同?

Re-querying the DOM for no reason is a waste, but most of the time it doesn't matter in any observable way, and especially not for ID selectors like $("#myDiv") which are optimized by jQuery into calls to getElementById (which is blazingly-fast). 无缘无故地重新查询DOM是一种浪费,但是大多数情况下,它以任何可观察的方式都无关紧要,尤其是对于像$("#myDiv")这样的ID选择器来说,这$("#myDiv") ,这些选择器由jQuery优化为对getElementById调用(速度非常快)。 So on the one hand, yes, it's wasteful extra work. 因此,一方面,是的,这是浪费大量的额外工作。 On the other hand, browsers are so fast these days that you probably have bigger fish to fry. 另一方面,如今的浏览器是如此之快,以至于您可能要炸更大的鱼。 But it's probably extra pointless code, which is the larger issue. 但这可能是多余的毫无意义的代码,这是更大的问题。

To the general point: jQuery is set-based . 总的来说:jQuery是基于集合的 This means that operations on sets with no elements in them are no-ops. 这意味着对没有元素的集合的操作是无操作的。 Eg: 例如:

$(".foo").addClass("bar");

...is a no-op (not an error) if no .foo elements are in the DOM. 如果DOM中没有.foo元素,则...是无操作(不是错误)。

So check for length if and when you care whether you matched elements. 因此,请在检查是否匹配元素时检查其length If you don't care and just want to perform operations on them if they're there , just go ahead and do the operation (with one important caveat 1 ). 如果你不在乎,只是想对他们的操作。如果他们在那里 ,只是继续前进,做了手术(有一个重要的警告1)。

So basically, there are three scenarios: 因此,基本上,存在三种情况:

  1. You know the elements will be there, so the check is pointless 您知道元素将在那里,所以检查毫无意义
    => no check =>不检查

  2. You don't know the elements will be there, but you don't care and just want to operate on them if they're there 你不知道的元素会在那里,但你不在乎,只是想,如果他们在那里对他们进行操作
    => no check =>不检查

  3. You care whether the elements are there for some other reason 您关心元素是否由于其他原因存在
    => do the check =>做检查


1 Here's the important caveat: If you're calling a jQuery function that returns something other than a jQuery object (for instance, val() or offset() or position() , etc.), when you call it on an empty set, it will typically return undefined (the exception is text() , which will return "" [ text() is not like the others in several ways; this is one of them]). 1这是重要的警告:如果您要调用的jQuery函数返回的是jQuery对象以外的其他内容(例如val()offset()position()等),则在对一个空集进行调用时,它通常会返回undefined (例外是text() ,它将返回"" [ text()在许多方面都不像其他(这是其中之一)))。 So writing code that naively assumes those things will return what you're expecting, even when the set is empty, will bite you. 因此,即使天真地假设集合是空的,编写天真的假设这些东西都会返回您期望的代码会咬住您。

So for example: 因此,例如:

if ($(".foo").offset().top > 20)

...will throw an error if there are no .foo elements, because offset() will return undefined , not an object. 如果没有.foo元素,则会抛出错误,因为offset()将返回undefined ,而不是对象。

But this is fine: 但这很好:

$(".foo").closest(".bar").toggleClass("baz"):

...because even if there are no .foo , closest() will return another empty jQuery set, and calling toggleClass() on it is a no-op. ...因为即使没有.fooclosest()也将返回另一个空的jQuery集,并且在其上调用toggleClass()是无操作的。

So when dealing with an accessor that doesn't return a jQuery object, if you don't know for sure you're dealing with a set containing at least one element, you need more defensiveness, eg: 因此,在处理不返回jQuery对象的访问器时,如果不确定是否要处理包含至少一个元素的集合,则需要更多防御性,例如:

var offset = $(".foo").offset();
if (offset && offset.top > 20)

Again, most accessors (that don't return jQuery objects) do this, including val() , offset() , position() , css() , ... 同样,大多数访问器(不返回jQuery对象)都这样做,包括val()offset()position()css() ,...

It matters, and $( "#myDiv" ).length is better because it is not running document.getElementById('myDiv') twice. 这很重要, $( "#myDiv" ).length更好,因为它没有两次运行document.getElementById('myDiv') If you were caching the selector, it would matter much less: 如果您要缓存选择器,那就没关系了:

var $myDiv = $( "#myDiv" );

if ($myDiv && $myDiv.length) { ... }

However, jQuery selectors always return something that will be interpreted as "truthy", so 然而,jQuery选择总是返回的东西 ,将被解释为“truthy”,所以

if ($('#myDiv')) { 

is a pointless check. 是没有意义的检查。

One guy I work with says the correct way to check should be: 与我一起工作的一个人说正确的检查方法应该是:

 if ($( "#myDiv" ) && $( "#myDiv" ).length ) { //do something } 

This statement is false. 这句话是错误的。 The check of $( "#myDiv" ).length would simply return 0 if it's not found. 如果没有找到$( "#myDiv" ).length返回0 To be sure, here's a code example with both an id found, and a second id that's not present: 可以肯定的是,这是一个代码示例,同时找到了一个id和一个不存在的第二个id

 console.log("Count of #myDiv's found: " + $("#myDiv").length); console.log("Count of #AnotherMyDiv's found: " + $("#AnotherMyDiv").length); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <div id="myDiv"></div> 

Depends on what you're doing after the if statement. 取决于if语句后的操作。

Think of it like this. 这样想吧。 Any jquery selector call returns a jquery object that's basically an array extended with a bunch of different methods. 任何jquery选择器调用都将返回一个jquery对象,该对象基本上是一个用一堆不同方法扩展的数组。 When you call something like $(".my-elements").show() it'll loop over whatever elements $(".my-elements") returned and apply .show() to them then return that same jquery object which is how jquery chains. 当您调用$(".my-elements").show()之类的东西时,它将遍历$(".my-elements")返回的任何元素,并将.show()应用于它们,然后返回相同的jquery对象, jQuery是如何链接的。

If you're just doing chainable things then no, you don't need to do the if check. 如果您只是在做可链接的事情,那么不,您不需要进行if检查。 Because all the subsequent methods will just do a loop equivalent to for (var i=0; i<0; i++){ } which means nothing will happen. 因为所有后续方法都只会执行与for (var i=0; i<0; i++){ }等效的循环,所以不会发生任何事情。 If you've got a big chain and you're running it in a big loop that needs to be very performant, you might want to do the if check, but generally it won't really matter from a performance standpoint. 如果您有一个大型连锁店,并且需要在一个很大的循环中运行它,并且需要非常高效,那么您可能需要进行if检查,但是从性能的角度来看,通常这并不重要。

If however, you're doing something that will throw an error or give you a bad result if the element doesn't exist then yes, use the if check. 但是,如果您执行的操作会引发错误,或者如果该元素不存在则会给您带来不好的结果,那么可以使用if检查。 For instance 例如

function addHeight() {
  var height1 = $("#div1").height(),
      height2 = $("#div2").height();

  $("#otherDiv").css({ top: height1 + height2 });
}

If div1 or div2 don't exist otherDiv will get it's top set to NaN which is probably not what you're going for. 如果div1div2不存在, otherDiv会将其最高设置为NaN ,这可能不是您想要的。 In this case you would want to verify that both div1 and div2 actually exist, but you still don't really have to check for otherDiv because if it doesn't exist nothing will happen anyway. 在这种情况下,您可能想验证div1div2确实存在,但是您仍然不必真正检查otherDiv因为如果不存在,则将不会发生任何事情。

Is more simple to check if exists and , if we want more info from the object we will not receive null object error. 检查是否存在和更简单,如果我们希望从对象中获取更多信息,则不会收到空对象错误。

if (!!$( "#myDiv" )) {
    //do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM