ducument.ready不生效的问题 ruby on rails
rails web app页面之间的跳转的时候使用ducument.ready只有在再次加载的时候才生效, 因为rails用了turbolinks,
https://github.com/turbolinks/turbolinks/blob/master/README.md#running-javascript-when-a-page-loads
Running JavaScript When a Page Loads
You may be used to installing JavaScript behavior in response to the window.onload
, DOMContentLoaded
, or jQuery ready
events. With Turbolinks, these events will fire only in response to the initial page load—not after any subsequent page changes.
In many cases, you can simply adjust your code to listen for the turbolinks:load
event, which fires once on the initial page load and again after every Turbolinks visit.
document.addEventListener("turbolinks:load", function() {
// ...
})
When possible, avoid using the turbolinks:load
event to add event listeners directly to elements on the page body. Instead, consider using event delegation to register event listeners once on document
or window
.
$(document).ready
依赖于DOMContentLoaded
事件- Turbolinks 接管页面后换页不会产生
DOMContentLoaded
,所以换页之后$(document).ready
无效 - Turbolinks 提供了
page:change
取代$(document).ready
- 现在第一次加载也会触发
page:change
了,所以page:change
可以替代$(document).ready
要注意 turbolinks 会缓存访问过的页面,缓存 restore 的时候也会触发 page:chang
,这样的代码在用户后退的时候会重复绑定:
$(document).on 'page:change', ->
$('body').on 'click', ->
console.log('hit')
page:change
和 $().ready
逻辑一样,区别是 turbolinks restore 的时候页面带着之前的状态,而传统页面后退的时候是干净的。
如果要用 turbolinks,最好就用 page:change
,并且要写可以反复执行不冲突的代码。
现在用 page:change
很少,换成这样写:
$(document).on 'click', 'body', ->
console.log('hit')
还有个 page:load
,考虑到 restore 的问题,page:load
才是对应 $().ready
document.addEventListener("turbolinks:load",
function() {
if ($('#component_component_type').attr("data-value") == "display_item") {
$('.display-item-select').show();
$('.grid-select').hide();
} else if($('#component_component_type').attr("data-value") == "grid") {
$('.grid-select').show();
$('.display-item-select').hide();
} else {
$('.grid-select').hide();
$('.display-item-select').hide();
}
}) $(document).on('change', '#component_component_type_id', function(e) {
if ($('#component_component_type_id').find("option:selected").text() == "display_item") {
$('.display-item-select').show();
$('.grid-select').hide();
} else if($('#component_component_type_id').find("option:selected").text() == "grid") {
$('.grid-select').show();
$('.display-item-select').hide();
} else {
$('.grid-select').hide();
$('.display-item-select').hide();
}
});
ducument.ready不生效的问题 ruby on rails的更多相关文章
- 10 steps to get Ruby on Rails running on Windows with IIS FastCGI- 摘自网络
Since the original tech preview release of FastCGI last year, we've been seeing a lot of requests fo ...
- ruby on rails在fedora18上install
ruby on rails 在fedora18下的安装 天朝的网络原因,安装不是很顺畅,所以把过程记录下备用 前面下载rubygem什么的都比较快,新建一个project的时候会出问题 gem new ...
- Fedora 16下安装ruby on rails
Fedora 16下安装ruby on rails 最近在windows下写了些rails小程序,问题一个接一个,到最后终于坚信了那句话“windows不适合用于ruby on rails开发”.于是 ...
- 用VirtualBox和vagrant在win7×64上搭建ruby on rails 开发环境
下载准备 1.vagrant 官方 WINDOWS Universal (32 and 64-bit) http://www.vagrantup.com/downloads.html 2.Virtu ...
- How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04
How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04 链接来自于:https://www.digitalo ...
- Linux超快速安装Ruby on Rails
Linux超快速安装Ruby on Rails 时间 2014-11-25 11:45:11 Flincllck Talk 原文 http://www.flincllck.com/quick-ins ...
- 管理不同版本ruby和rails的利器——rvm
近年来,ruby on rails逐渐火了起来,我想各位码农早就耳闻,特别是那些做B/S项目的童鞋,早就想跃跃一试了. 笔者也是初次接触ruby on rails ,我想,对于初学者来说,最好的学习方 ...
- ruby on rails on windows
这次想系统学会rails,最终目标是将redmine改造成顺手的工具,主要的手段就是开发redmine插件.虽然网上都推荐使用类Unix系统,可手头只有win7系统,就安装了. 难免会遇到这样那样的问 ...
- win8平台下Ruby on Rails的第一个web应用
最近在做一个网站web前端的前期开发,老板要求用Ruby on Rails搭建部署开发环境,上网搜之,发现整个搭建流程比较坑爹,于是用了一款集成软件Bitnami Ruby Stack一键安装到我的w ...
随机推荐
- BZOJ2286: [Sdoi2011]消耗战
建出虚树dp. 把询问点按dfs序排序,用一个以dfs序为关键字的单调栈(以深度为关键字也是一样的),每次将一个询问点与栈顶的点的lca入栈,再将这个询问点入栈,在这个过程中建出一棵树就是虚树.具体看 ...
- Linux 中,如何显示 (gcc)make时实际执行命令
问题: 调试编译问题,如何获取,GCC(或许make)时,实际编译器和链接器正在执行的命令? 解决方法: 方法一:通用方法 使用dry run,如下 $ make -n 这将显示make 命令正在试图 ...
- HashMap与ArrayList互相嵌套的代码实现
HashMap嵌套ArrayList的代码实现 结果要求为 三国演义 吕布 周瑜笑傲江湖 令狐冲 林平之神雕侠侣 ...
- bootstrap弹框
http://v3.bootcss.com/javascript/#modals 参考bootstrap官网 模态框做php后端 前端一直不行,但是很多时候 用到ajax都要用到弹框,一直在代码里面找 ...
- JavaScript Engines
http://openaphid.github.io/blog/2013/01/17/part-i-how-to-choose-a-javascript-engine-for-ios-and-andr ...
- Unity multi_compile
http://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html http://forum.unity3d.com/threads/tuto ...
- Linux基础整理-软件的安装与卸载
redhat/centos/fedora/suse系列: 摘自网址:http://www.runoob.com/linux/linux-yum.html yum( Yellow dog Updater ...
- Apache配置--用户认证(针对目录访问)-update2015-05-02
通过Apache配置可以限制用户对目录的访问,会弹出像phpadmin一样的登陆框. ========================================================= ...
- [问题] UISearchBar 点击取消后跳动的问题
问题详情: 首先是TableView 作为 NavigationController 的 RootViewContrller, 然后UISearchBar 添加到TableView 的 headV ...
- 仿微信底部自定义菜单 移动web
最近在做微信开发,要实现微信公众号改版—-改成微官网形式,即移动web页面中实现公众号的主页面,包括了公众号的菜单在底部显示 本文针对仿公众号底部菜单这个功能实现进行总结.实现采用html和css.J ...