我是 JavaScript 和 Rails 新手。我正在 ruby on Rails 中构建一个网站,并尝试在 View 中使用 JavaScript。我需要隐藏和显示表格的一些元素,我有一些脚本任务,它仅适用于表格的第一行。有没有更好的方法来做到这一点谢谢您的帮助。我需要每个按钮来显示或隐藏表格中相应的元素
应用程序.html
<!DOCTYPE html>
<script>
function changeColor(newColor) {
document.getElementById("para1").style.display = "none"
document.getElementById("para2").style.display = "none"
document.getElementById("para3").style.display = "none"
document.getElementById("para4").style.display = "block"
document.getElementById("para5").style.display = "none"
}
function show(newColor) {
document.getElementById("para1").style.display = "block"
document.getElementById("para2").style.display = "block"
document.getElementById("para3").style.display = "block"
document.getElementById("para4").style.display = "none"
document.getElementById("para5").style.display = "block"
}
</script>
<html>
<head>
<title>VincentricData</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag "defaults", media: "all", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
index.html
<h1>Listing vehicles</h1>
<table>
<thead>
<tr>
<th>Year</th>
<th>Make</th>
<th>Model</th>
<th>Msrp</th>
<th>Rating</th>
<th>Total operating costs</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @vehicles.each do |vehicle| %>
<tr>
<td><%= vehicle.year %></td>
<td><%= vehicle.make %></td>
<td><%= vehicle.model %></td>
<td><div id="para1" ><%= vehicle.msrp %></div></td>
<td><div id="para2" ><%= vehicle.rating %></div></td>
<td><div id="para3" ><%= vehicle.total_operating_costs %></div></td>
<td><div id="para4" ><button onclick="show('#00FFFF');">Show</button></div></td>
<td><div id="para5" ><button onclick="changeColor('#00FFFF');">hide</button></div></td>
<td><%= link_to 'Edit', edit_vehicle_path(vehicle) %></td>
<td><%= link_to 'Destroy', vehicle, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Vehicle', new_vehicle_path %>
最佳答案
首先,如果您的行处于循环中,则无法为元素提供硬编码 ID,因为会有多个 HTML 元素具有相同的 ID,这绝对是非常糟糕的。 为您的元素指定一个类,该类可以根据需要重复多次。
其次,您可以(如果可能的话应该)使用 jQuery 来完成此类事情。 它真的比纯 JavaScript 更直观、更容易。
<script src='/somewhere/jquery.min.js'></script>
<script>
(function ($) {
// Here you are sure that jQuery is included in your HTML page
$(function () {
// Now the DOM is fully loaded, you can use some javascript without problem
// Listen a click on each ".para4" element : (class='para4')
$('.para4').on('click', function (event) {
// Get the parent <tr> element
var $tr = $(this).closest('tr');
// On this parent <tr>, display all .para elements
$tr.find('.para').show();
/*
This is equivalent to :
$tr.find('.para').css('display', 'block');
*/
// Then hide the .para4 one only.
$tr.find('.para4').hide();
/*
This is equivalent to :
$tr.find('.para4').css('display', 'none');
*/
});
// Listen a click on each ".para5" element : (class='para5')
$('.para5').on('click', function (event) {
// Get the parent <tr> element
var $tr = $(this).closest('tr');
// On this parent <tr>, hide all .para elements
$tr.find('.para').hide();
/*
This is equivalent to :
$tr.find('.para').css('display', 'none');
*/
// Then display the .para4 one only.
$tr.find('.para4').show();
/*
This is equivalent to :
$tr.find('.para4').css('display', 'block');
*/
});
});
})(window.jQuery);
</script>
fiddle :http://jsfiddle.net/9uRps/
如果没有 jQuery,您必须使用循环和事件监听器:
// Find all your .para4 elements
var para4Array = document.getElementsByClassName('para4');
var para4Length = para4Array.length;
// For each of them, listen for a click event
while (para4Length--) {
para4Array[para4Length].addEventListener('click', function (event) {
// Find the parent's <tr>
var tr = this.parentNode.parentNode;
// Display all .para children of this <tr>
tr.querySelectorAll('.para').style.display = 'block';
// Then hide the .para4 one
tr.querySelectorAll('.para4').style.display = 'none';
});
}
// Find all your .para5 elements
var para5Array = document.getElementsByClassName('para5');
var para5Length = para5Array.length;
// For each of them, listen for a click event
while (para5Length--) {
para5Array[para5Length].addEventListener('click', function (event) {
// Find the parent's <tr>
var tr = this.parentNode.parentNode;
// Hide all .para children of this <tr>
tr.querySelectorAll('.para').style.display = 'none';
// Then display the .para4 one
tr.querySelectorAll('.para4').style.display = 'block';
});
}
关于javascript - 在 Rails 应用程序中使用 Javascript 显示和隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25019885/