Javascript 使用HTML/CSS/JS的Rails应用程序中的过滤器元素
遵循创建HTML/CSS/JS过滤器的说明后: 最初没有显示任何项目。我想只有等到Javascript 使用HTML/CSS/JS的Rails应用程序中的过滤器元素,javascript,jquery,html,css,ruby-on-rails,Javascript,Jquery,Html,Css,Ruby On Rails,遵循创建HTML/CSS/JS过滤器的说明后: 最初没有显示任何项目。我想只有等到 btns[i].addEventListener(“单击”,函数(){)在过滤开始工作时被触发。我假设这与Rails应用程序或JavaScript中的JavaScript执行方式有关,因为我在这方面缺乏足够的经验 提前感谢您的帮助 以下是我当前使用的代码: application.js // listed below. // // Any JavaScript/Coffee file within this di
btns[i].addEventListener(“单击”,函数(){
)在过滤开始工作时被触发。我假设这与Rails应用程序或JavaScript中的JavaScript执行方式有关,因为我在这方面缺乏足够的经验
提前感谢您的帮助
以下是我当前使用的代码:
application.js
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require rrt
//= require_tree .
filterSelection("all")
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName("filterDiv");
if (c == "all") c = "";
// Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}
// Show filtered elements
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
element.className += " " + arr2[i];
}
}
}
// Hide elements that are not selected
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
// Add active class to the current control button (highlight it)
var btnContainer = document.getElementById("myBtnContainer");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
感谢kiso.io的John McDowell 在一个新文件custom.js中并从application.js中删除require_tree,因为我使用的是TurboLink
filterSelection(“all”)
需要像这样包装
$(document).on('turbolinks:load', function() {
filterSelection("all")
})
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName("filterDiv");
if (c == "all") c = "";
// Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}
// Show filtered elements
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
element.className += " " + arr2[i];
}
}
}
// Hide elements that are not selected
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
// Add active class to the current control button (highlight it)
var btnContainer = document.getElementById("myBtnContainer");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
$(document).on('turbolinks:load',function(){
过滤器选择(“全部”)
})
函数过滤器选择(c){
变量x,i;
x=document.getElementsByClassName(“filterDiv”);
如果(c==“全部”)c=“”;
//将“show”类(display:block)添加到过滤的元素中,并从未选择的元素中删除“show”类
对于(i=0;i-1)w3AddClass(x[i],“show”);
}
}
//显示筛选的元素
函数w3AddClass(元素、名称){
变量i,arr1,arr2;
arr1=element.className.split(“”);
arr2=name.split(“”);
对于(i=0;i-1){
arr1.拼接(arr1.indexOf(arr2[i]),1);
}
}
element.className=arr1.join(“”);
}
//将活动类添加到当前控件按钮(高亮显示)
var btnContainer=document.getElementById(“myBtnContainer”);
var btns=btnContainer.getElementsByClassName(“btn”);
对于(变量i=0;i
.container {
overflow: hidden;
}
.filterDiv {
display: none; /* Hidden by default */
}
/* The "show" class is added to the filtered elements */
.show {
display: block;
}
$(document).on('turbolinks:load', function() {
filterSelection("all")
})
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName("filterDiv");
if (c == "all") c = "";
// Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}
// Show filtered elements
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
element.className += " " + arr2[i];
}
}
}
// Hide elements that are not selected
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
// Add active class to the current control button (highlight it)
var btnContainer = document.getElementById("myBtnContainer");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}