在Iron Router中的this.render()之后访问HTML [英] Access HTML after this.render() in Iron Router
问题描述
我需要在新渲染的模板上应用一些语义UI,但是我不知道如何从路由器获取渲染的HTML.这是我的代码:
I need to apply some Semantic UI on newly rendered templates, but I have no idea how to get the rendered HTML from the router. Here's my code :
Router.route('/', {
name: 'home',
where: 'client',
action: function () {
this.render('home');
},
onAfterAction: function () {
console.log( $('#homeSidebar') );
}
});
基本上,$('#homeSidebar')
应该返回侧边栏元素,但由于HTML尚不可用,因此不返回任何内容.到目前为止,唯一的解决方案是像这样更改功能
Basically, $('#homeSidebar')
should return the sidebar element, but it returns nothing as the HTML is not yet available. The only solution, so far, was to change the function like this
onAfterAction: function () {
setTimeout(function () {
$('#homeSidebarToggle').on('click', function () {
$('#homeSidebar').sidebar('toggle');
});
}, 200);
}
既不干净也不安全. HTML可用后如何立即运行功能?
Which is neither clean nor safe. How can I run a function immediately as soon as the HTML is availble?
推荐答案
您应在模板上使用onRendered
回调.流星使用模板helpers, events and callbacks
来操作DOM.铁路由器仅用于路由)
您的client/view.html:
You should use onRendered
callback on the template. Meteor uses template helpers, events and callbacks
for manipulating DOM. Iron Router is just for routing)
Your client/view.html:
<template name="home">
<div id="homeSidebar">
</div>
</template>
您的client/view.js:
Your client/view.js:
Template.home.onRendered(function () {
console.log($('#homeSidebar'));
});
这篇关于在Iron Router中的this.render()之后访问HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!