Is it possible to use multiple variables instead of selectors in jQuery

前端 未结 9 2499
半阙折子戏
半阙折子戏 2020-12-07 20:03

I know that it\'s faster to do the following:

var $header = $(\"#header\");
$header.css({color:\"#ff0000\"});
$header.find(\"a\").addClass(\"foo\");
<         


        
9条回答
  •  没有蜡笔的小新
    2020-12-07 20:55

    Just use the add method:

    $header_elements.add($footer_elements).css({color:'#ff0000'});
    

    Given a jQuery object that represents a set of DOM elements, the .add() method constructs a new jQuery object from the union of those elements and the ones passed into the method. The argument to .add() can be pretty much anything that $() accepts, including a jQuery selector expression, references to DOM elements, or an HTML snippet.

提交回复
热议问题