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

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742333571a4424200.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信