在我的应用程序中,仅当页面完全加载时才显示表格页面。应用程序中有两个这样的页面。为了实现这一点,我在相应 View 中为表格设置了“style =“display:none””,当页面完全加载时,我使用“$('mytable').show()”通过Javascript文件显示表格。第一页有指向其他类似页面的链接,该页面应显示表格,但是当我单击该链接时,其他页面上的表格仍然隐藏。我知道发生这种情况是因为 Javascript(取消隐藏表)不会再次执行。如何在这两个页面之间共享 Javascript?请注意,我还有其他 Javascript 代码,我想在这两个页面之间共享它们。
已知解决方案: 删除“turbolinks”对我来说很有效,但每次我在页面之间切换时,加载速度都非常慢。有没有办法让这项工作不影响性能。
我的 Javascript 位于“app/assets/javascripts/myscript.js”。
我有一个 Controller “mycontroller”和两个操作,“index”与 View “view1”和“index1”与 View “view2”。这两个 View 都显示具有不同数据的相似表格。
我尝试在互联网上搜索,但找不到相关帖子。
如果我需要分享更多详细信息,请告诉我。
感谢您的帮助!
解决方案:我在 Rails Guide 上找到了解决方案,请参阅下面的答案。
最佳答案
直接来自Rails guide :
5.2 Page Change Events When writing CoffeeScript, you'll often want to do some sort of processing upon page load. With jQuery, you'd write something like this:
$(document).ready -> alert "page has loaded!"
However, because Turbolinks overrides the normal page loading process, the event that this relies on will not be fired. If you have code that looks like this, you must change your code to do this instead:
$(document).on "turbolinks:load", -> alert "page has loaded!"
For more details, including other events you can bind to, check out the Turbolinks README.
只需修改该示例即可重新加载表,而不是发出警报。
关于javascript - Rails 4 中使用 Turbolink 重定向到其他页面时重新加载 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37857322/