gpt4 book ai didi

javascript - Rails 应用程序中不显眼的 js 无法按预期工作

转载 作者:行者123 更新时间:2023-11-30 00:12:17 25 4
gpt4 key购买 nike

我有这样的链接:

<%= link_to "Load More", comments_feed_path(@post.id), :id => 'my-link', :remote => true %>

在 application.js 文件中:

$( document ).ready(function() {

$('#my-link').bind('click', function() {
alert('Hooray!');

//event.preventDefault(); // Prevent link from following its href
});
});

但什么也没有发生。如果我这样做,它会按预期工作,但我宁愿使用不显眼的 js ...

<%= link_to "Load More", comments_feed_path(@story.id), :id => 'my-link', :onclick => "test()", :remote => true%>

应用程序.js:

function test(){
alert('test');
}

关于我遗漏的任何想法?

最佳答案

似乎是一个常见的 turbolinks 问题。 Turbolinks 是 gem,默认情况下包含在 rails 中,它可以防止在每次请求时加载页面的公共(public)部分。它阻止重新加载的最常见部分是 header ,包括所有 javascript 定义。这意味着,您的 $(document).ready 回调仅针对应用程序的初始页面执行。

要修复它,您需要将 $(document).ready 更改为:

$(document).on('turbolinks:load', function() {

阅读文档:https://github.com/turbolinks/turbolinks

关于javascript - Rails 应用程序中不显眼的 js 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com