javascript - JavaScript 在 Ruby on Rails 应用程序中不起作用
问题描述
我想用 JS 和 CSS 为我的应用程序制作一个可折叠的侧边菜单。这是我的代码:
在 views/pages/home.html.erb 我有:
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application','data-turbolinks-track': 'reload' %>
<div class = "head">
<div class = "navbar">
<div id="mySidepanel" class="sidepanel">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<button class="openbtn" onclick="openNav()">☰ Toggle Sidepanel</button>
<h1 class = "titles">
<%if current_user%>
¡Bienvenido <%=current_user.first_name%>!
<%else%>
Home
<%end%>
</h1>
<h3 class = "secondary-contact">25 de mayo 1160 Puiggari, Entre Rios, Argentina</h3>
<h3 class = "secondary-contact">TEL. 0343 15-467-5058</h3>
</div>
我的样式表/application.css
.sidepanel {
height: 250px; /* Specify a height */
width: 0; /* 0 width - change this with JavaScript */
position: fixed; /* Stay in place */
z-index: 1; /* Stay on top */
top: 0;
left: 0;
background-color: #111; /* Black*/
overflow-x: hidden; /* Disable horizontal scroll */
padding-top: 60px; /* Place content 60px from the top */
transition: 0.5s; /* 0.5 second transition effect to slide in the sidepanel */
}
/* The sidepanel links */
.sidepanel a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
/* When you mouse over the navigation links, change their color */
.sidepanel a:hover {
color: #f1f1f1;
}
/* Position and style the close button (top right corner) */
.sidepanel .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
/* Style the button that is used to open the sidepanel */
.openbtn {
font-size: 20px;
cursor: pointer;
background-color: #111;
color: white;
padding: 10px 15px;
border: none;
}
.openbtn:hover {
background-color: #444;
}
在 javascript/packs/application.js 我添加了:
function openNav() {
document.getElementById("x").style.width = "15%";
}
function closeNav() {
document.getElementById("mySidepanel").style.width = "0";
}
问题是当我单击按钮时没有任何反应。我对 JavaScript 很陌生,我找不到我的错误。
解决方案
您不能直接从 dom 访问 application.js 或其他 js 文件函数。因为 rails 使用 webpack 来保存你的代码。您可以使用这样的事件;
let openButton = document.querySelectorAll('button.openbtn')
openButton.addEventListener("click", function(event) {
document.getElementById("x").style.width = "15%";
})
let closeButton = document.querySelectorAll('button.closebtn')
closeButton.addEventListener("click", function(event) {
document.getElementById("mySidepanel").style.width = "0";
})
推荐阅读
- c# - 应用程序工作并冻结时更改表单
- pandas - 将数据框添加到特定行和列的现有数据框
- azure - Azure VM 到 Azure Blob 存储数据传输成本
- python - 更改熊猫数据框中随机选择的行的顺序
- google-colaboratory - Google Colab 没有实现模型训练
- python - 在 Python 中使用 QtGui.QPainter() 更改点对象的颜色
- xamarin.forms - Xamarin Forms Shell 标题栏颜色在深色主题中未正确设置
- javascript - 使用js打印图案
- javascript - 过渡效果仅在 Firefox 中有效
- php - 图像不保存到临时文件夹但保存到数据库