gpt4 book ai didi

jquery - 在 Ruby on Rails 中,如果 View 需要使用在应用程序布局末尾加载的 jQuery,该怎么办?

转载 作者:行者123 更新时间:2023-12-01 06:30:33 24 4
gpt4 key购买 nike

一种推荐的做法是在应用程序布局末尾加载 jQuery:

= yield

= render 'shared/footer'

= javascript_include_tag 'jquery'

使页面渲染速度更快,而不受 Javascript 或 jQuery 的阻碍。但是如果每个 View 都需要做一些特定的事情怎么办?例如,index View 需要执行某些操作,show View 需要执行其他操作,因此 Javascript 代码要么位于这些 View 模板内,要么包含在其中从这些 View 模板中,但此时,jquery.js 文件尚未加载(直到接近页面末尾)? jquery.js 可以在 yield 之前加载,但它位于页面前面。

最佳答案

您可以使用 content_for

布局中

yield
render 'footer'
javascript_include_tag 'jquery'
yield :custom_javascript

在 View 中

<%= content_for :custom_javascript do %>
<script type="text/javascript">alert(jQuery)</script>
<% end %

http://guides.rubyonrails.org/layouts_and_rendering.html#using-content_for

关于jquery - 在 Ruby on Rails 中,如果 View 需要使用在应用程序布局末尾加载的 jQuery,该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5676757/

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