使用pjax时在哪里放置页面初始化javascript? [英] Where to put the page-initialize javascript when using pjax?
问题描述
大多数时候,我在 $(document).ready
中放了一些javascript代码来在页面上做一些初始化的事情,比如事件绑定等。
Most of the times, I put some javascript code in $(document).ready
to do some initialization stuffs on the page, like event binding, etc.
但现在我想使用 pjax
https://github.com/defunkt/jquery-pjax 。
使用pjax,因为只有部分页面被刷新, $(document).ready
将无法获得又叫了一遍。
With pjax, because there's only part of the page gets refreshed, $(document).ready
will not get called again.
我可以在事件 pjax:end
上手动触发初始化脚本,但我也想知道是否有更好的解决方案。
I could manually trigger the initializing script on event pjax:end
, but I also want to know if there's a better solution for that.
谢谢。
推荐答案
我认为你的最好的选择可能只是做你所说的,并将你的页面特定的初始化代码挂钩到pjax:end事件。这样,它基本上可以完成它之前所做的相同工作,并且可以对付加载到DOM中的任何html。
I think your best bet is probably just to do what you said and hook your page specific init code up to the "pjax:end" event. That way it'll essentially be doing the same job that it did before and run against whatever html has been loaded in to the DOM.
这篇关于使用pjax时在哪里放置页面初始化javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!