Rails 在 Rails 7 中 跳转后 js 代码失效问题

lobb · May 18, 2023 · Last by daqing replied at May 18, 2023 · 213 hits
$(document).ready(function){
    $('[data-toggle="popover"]').popover();
}

 window.onload = function(){
   $('[data-toggle="popover"]').popover();
 }

在跳转链接后上面的 js 代码都失效了

解决 document.addEventListener("turbo:load",() => { $('[data-toggle="popover"]').popover(); })

rails7 使用的是 turbo 查看 js 代码是被加载到页面的,但不知道为何会失效。

有了 turbo 之后,$(document).ready()就不会触发了,需要监听turbo:load事件。

You need to Sign in before reply, if you don't have an account, please Sign up first.