How do I cache jquery selectors?

demand:

var str = $("-sharpdiv1 .list");

after caching the above code, how should I reuse str if I want to use $("- sharpdiv1. List td") later in the coding process?


try the following code:

var str = $('-sharpdiv1 .list');
var td = str.find("td");
//
var td = str.children("td");

I don't know what it means!
first: the following code can get str either by declaring a variable before str, or by making a function call.
second: if you want to use this code, then the result of this code is str.
third: save the code var str = "$('- sharpdiv1. List')". Use eval ($('- sharpdiv1 .list'));


)

use it as the second parameter of the jQuery selector

$("td", str);

you can define a this.els object variable:

this.els = {
    dom1: $('-sharpdiv1 .list'),
    dom2: $('-sharpdiv2 .list')
}

this allows you to refer directly to this.els.dom1 (which requires the corresponding dom) when you need to use it anywhere else on the page. Define it once, use it many times. You don't need to use the jquery selector $('- sharpdiv1. List') to get it again on the next reference.

Menu