我有一个习惯js
文件输入app/assets/javascripts
。
这是js文件:
//app/assets/javascripts/contacts.js
//$(document).ready(function() { //I've already tried with this method
$(window).load(function() {
alert("foo bar")
});
我需要文件contacts.js
文件在application.js
file.
如果我检查 html 页面,我会看到 js 文件已正确加载,但未显示该消息。
如果我重新加载页面(按 f5),该消息将正确显示。
当页面加载时,JavaScript 会被加载(我可以在浏览器的源 html 代码中看到它)但不会执行。
你能帮助我吗?
解决方案:
Rails 4:如何将 $(document).ready() 与 Turbo-links 一起使用 https://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links
$(document).ready(function() { } # Not working with turbo-links
From http://guides.rubyonrails.org/working_with_javascript_in_rails.html#turbolinks
Turbolinks 会覆盖正常的页面加载过程,该事件
这靠的是不会被解雇。如果你的代码看起来像
为此,您必须更改代码才能执行此操作:
$(document).on('ready page:load', function () {
// you code here
});
另一个问题 https://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)