What is the difference between $ ("# context.searched") and $ (". Search", $ ("# context"))?

I had this question in my memory for a long time.

Theoretically, the core jQuery function assumes an optional value, which can be a DOM element - $(".searched",$("#context")[0])- or a jQuery - object $(".searched",$("#context") ). I found that the last question reading this article.

But I really don't see the difference between using context and passing in a more complex css expression. If there is no difference in how it works, is there a difference in performance?

thank

+3
source share
3 answers

, $(".searched", context); . $(context).find('.searched'); , , , .

:

$.fn.highlightSearch = function() {
  return this.each(function() {
    $('.searched', this).addClass('highlighted');
    // the commented line performs the same thing:
    // $(this).find('.searched').addClass('highlighted');
  });
};

$('#context').highlightSearch();
$('.somethingElse').highlightSearch();

, .

$('#context'), $('#context .searched') . , $('#context'), , .find(select) $(selector, context) .

+2

DOM ( Sizzle, ) , a .find() .

( ?), :

$("#context .searched")

jQuery:

$("#context")[0]

, jquery, jquery .find(), , , :)

+3

: CSS, $("#context .searched"), , .

0

Source: https://habr.com/ru/post/1761775/


All Articles