Thursday 15 May 2014

javascript - Returning a function if an element exists -


I am looking for the most efficient way to break a function if a section of the element is already present in the DOM.

For example, if a class of span is present, at this time I am using

  $ (function () logic), if ( $ (This) (search 'span.myspan'. Length) returns, // logic will be executed if search span.myspan is not found}};   

I have seen some examples, Which use instead of / or others. Some examples that were using search ()

 . Find ('span.myspan') [0]   

Instead of checking there is nothing there to present it. ? Or maybe to compare it to an illiterate?

Then from within a function, if any element is present in the DOM and if this happens, the most effective method of returning the function immediately What is it?

Use vanilla js

  console.time ('T'); document.querySelector ('label key'); Console.timeEnd ('t') T: 0.000ms console.time ('t'); $ ('. Label key'); Console.timeEnd ('T') t: 1.000ms   

Jsperf test

In addition to check in another way that the element does not exist and then do stuff if it only leaves it alone.

  $ (function () {// logic will be executed if (! Document.query selector ('label-key');) {// logic argument executed if search span.myspan} }} Does not get;   

Really much more of a style.

No comments:

Post a Comment