最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor - add class to DOM element on template load - Stack Overflow

programmeradmin1浏览0评论

I'm trying to add a class to a DOM element that is the parent of all my DOM tree. I tried different approaches:

//this one doesn't work at all. DOM is not loaded
Template.home.created = function() {
    Meteor.startup(function() {
        $("#wrapper").addClass('app');
   });
}

//this one only works if you do a page load, not if you render the template through a link (using router)
Template.home.created = function() {
    Meteor.startup(function() {
        $(document).ready(function() {
            $("#wrapper").addClass('app');
        });
   });
}

//also does not work if I click on a link
Template.home.created = function() {
    $(document).ready(function() {
        $("#wrapper").addClass('app');
   });
}

//does not work at all (I really expected this one to work by clicking on a link (using router))
Template.home.created = function() {
    $("#wrapper").addClass('app');
   });
}

What I'm trying to do couldn't be more simple: add a class so I can style my wrapper accordingly to each template. Any advise on how to do this would be greatly appreciated.

I'm trying to add a class to a DOM element that is the parent of all my DOM tree. I tried different approaches:

//this one doesn't work at all. DOM is not loaded
Template.home.created = function() {
    Meteor.startup(function() {
        $("#wrapper").addClass('app');
   });
}

//this one only works if you do a page load, not if you render the template through a link (using router)
Template.home.created = function() {
    Meteor.startup(function() {
        $(document).ready(function() {
            $("#wrapper").addClass('app');
        });
   });
}

//also does not work if I click on a link
Template.home.created = function() {
    $(document).ready(function() {
        $("#wrapper").addClass('app');
   });
}

//does not work at all (I really expected this one to work by clicking on a link (using router))
Template.home.created = function() {
    $("#wrapper").addClass('app');
   });
}

What I'm trying to do couldn't be more simple: add a class so I can style my wrapper accordingly to each template. Any advise on how to do this would be greatly appreciated.

Share Improve this question asked Oct 20, 2013 at 4:28 Lucas LobosqueLucas Lobosque 3992 gold badges5 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The template created method is called when an instance of the template is initialized but does not wait for the DOM to be ready for manipulation.

Use the template rendered method which will get called when the DOM has been rendered by the template (once for first load, and every time the markup changes within the template)

Something like this should work (haven't tested):

Template.home.rendered = function(){
    var element = $("#wrapper");
    if(!element.hasClass("app")){
        element.addClass("app"); 
    }
}

Try using Template.home.rendered instead of Template.home.created. Don't use Meteor.startup or $(document).ready within it.

http://docs.meteor./#template_rendered

Template.home.created is called when the template object is created, but nothing has been rendered into dom nodes at that point.

发布评论

评论列表(0)

  1. 暂无评论