首页 技术 正文
技术 2022年11月21日
0 收藏 595 点赞 3,465 浏览 2208 个字

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.onloadDOMContentLoaded, or jQuery readyevents. 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.

  1. $(document).ready 依赖于 DOMContentLoaded 事件
  2. Turbolinks 接管页面后换页不会产生 DOMContentLoaded,所以换页之后 $(document).ready 无效
  3. Turbolinks 提供了 page:change 取代 $(document).ready
  4. 现在第一次加载也会触发 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();
}
});

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,943
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,469
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,283
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,098
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,729
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,766