How do I store multiple jQuery selectors in a javascript variable?

后端 未结 5 1540
野性不改
野性不改 2021-01-25 12:54

Obviously it\'s a good idea to store jQuery selectors in variables if they are used more than once (not a good idea if used only once).

My question is, how do y

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-25 13:45

    Not completely sure I understand what you mean. You want to store the jQuery objects so you don't have to continually search for them like some sort of cache?

    var cache = new Object();
    cache['#object1'] = $('#object1');
    cache['#object2'] = $('#object2');
    

    To retrieve the jQuery values, you'd merely have to call cache['#object1'].

提交回复
热议问题