我有一个home_controller
index
action. 并且是首页 root 'home#index'
.
index 这个首页上有个按钮,当点击的时候调用 js 代码(coffee)里面的代码来弹窗。
但是直接打开 localhost 时候没有反应,点击下菜单的 Home 时候再次点击这个按钮 coffee 代码被正常调用。不知道原因求高手指点。
菜单 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