【发布时间】:2016-08-11 07:05:29
【问题描述】:
我正在使用 Rails 5。我有一个带有索引操作的 home_controller 和 root 'home#index'。 index.html.erb上有一个按钮,我想按下这个按钮调用一些js代码(弹出窗口)。
但是我的咖啡脚本代码只有在我按下 Home 菜单后才被调用。所以我想知道/ 和home#index 有什么不同吗?感谢您的帮助。
application.html.erb
<nav>
<li><%= link_to "Home", "/"%></li>
</nav>
index.html.erb
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
<%= render :partial => 'stories/stories' %>
home.coffee
modal = document.getElementById('myModal');
btn = document.getElementById("myBtn");
span = document.getElementsByClassName("close")[0];
btn.onclick = ->
modal.style.display = "block"
span.onclick = ->
modal.style.display = "none"
window.onclick = (event) ->
modal.style.display = "none" if event.target == modal
【问题讨论】:
标签: ruby-on-rails coffeescript