How to properly cache DOM element in jquery?

前端 未结 2 1428
陌清茗
陌清茗 2021-01-15 13:19

I am having some trouble accessing cached DOM element from the namespace variable. My FeedManager config variable is like this:

var FeedManager = {
    confi         


        
2条回答
  •  有刺的猬
    2021-01-15 13:48

    Is your #feedContainer element present in your HTML or is it fetched later? If it is fetched later than you can't cache the element, you'd have to use just a selector string like feedContainer: '#feedContainer'`

    in your FeedManager object and then use something like

    $(FeedManager.config.feedContainer).append(li);
    

    If it is present in HTML then make sure that you define your FeedManager object after the DOM is ready, ie. inside of a

    $(document).ready(function () {
        // ...
    });
    

    or $(function () { ... });

提交回复
热议问题