jquery - Jquery:以前可以工作但不再起作用的功能
问题描述
我一直在为学校设计一个网站并编写 html 和 css。我不知道 jquery,所以有人帮助了那部分。一切都运行良好。但后来我添加了另一个函数 displayImages (在另一位朋友的帮助下),这使得以前的部分代码不再工作。这很可能是一个超级简单的错误,但我不知道如何解决它,所以非常感谢任何帮助!网站:http ://brand.housingcrisis.nl/
两个问题:
在左上角,只要选择了一个标签,就会显示一个(字)计数器。它曾经工作正常,但现在计数器始终保持在“显示:无”状态。我想这与代码中使用的活动类有关吗?
选择标签或切换一个按钮时,中心的灰色文本应该会逐渐消失一些(变成较轻的灰色)。这也不再起作用了,但是在我添加图像显示之前它就开始不起作用了。
jQuery 代码在这里(我只放了 2 个侧边栏项,但实际代码中有 10-15 个)我已经留下了第一个帮助的朋友的评论,因为我想它可能会有所帮助。
function displayImages() {
var x = document.getElementById("images-grid");
if (x.style.display == "none" || x.style.display == "") {
x.style.display = "block";
var e = document.getElementsByClassName("image-toggle");
e.classList.toggle("active");
} else {
x.style.display = "none";
}
}
var sidebarElementType = 'h4';
var sidebarItems = [{
title: 'students ',
tag: 'students',
count: 64,
}, {
title: 'investors',
tag: 'investors',
count: 143,
}];
function createSideBarItems() {
const sidebarSelector = document.querySelector('div.sidebar .items');
sidebarItems.forEach((item) => {
const newElement = document.createElement(sidebarElementType);
newElement.setAttribute('data-tag', item.tag);
newElement.classList.add('clickable-tag');
newElement.innerText = item.title
sidebarSelector.appendChild(newElement);
});
}
function on() {
document.getElementById("overlay-box").style.display = "block";
}
function off() {
document.getElementById("overlay-box").style.display = "none";
}
/** ------ START: SIDEBAR CATEGORY HANDLERS ----- */
/*
Handles toggling of the category in the main text. First it will
find all the existing active items and deactivate them, then it
will only add the active class to the items of the newly selected
category. This only will be activated if the clicked item on the
left is in an active state (e.g. if the item was active and you
click it again it will deactivate all the items along with itself)
*/
function toggleCategoryItem(className, isActive) {
// remove all others
document.querySelectorAll('.main span.active').forEach(el => {
el.classList.remove('active');
});
document.querySelector('.main').classList.remove('has-active-category');
// add active class if is activated
if (isActive) {
document.querySelector('.main').classList.add('has-active-category');
document.querySelectorAll(`.main .${className}`).forEach(el => {
el.classList.add('active');
});
}
}
/**
This will find the counter element in the main text, and will
find the provided category information based on the tag in the
first argument when this function is called. First it will
remove the active class (for the toggling effect), and then add
it back again if there is a category active currently with the
count of the active category set as innertext.
Next it will find the non-addressed-overlay item, and will toggle
this on if the newly found category has a count of 0.
*/
function adjustCount(item, isActive) {
const counterEl = document.querySelector('.main .counter');
counterEl.classList.remove('active');
// find selected item by tag id, if found then update counter;
const selectedItem = sidebarItems.find(i => i.tag === item);
if (selectedItem) {
counterEl.innerText = `Count: ${selectedItem.count}`;
}
const overlayEl = document.querySelector('.non-addressed-overlay');
overlayEl.classList.remove('active');
if (isActive) {
counterEl.classList.add('active');
if (selectedItem && selectedItem.count === 0) {
overlayEl.classList.add('active');
}
}
}
/**
This creates events listeners for the category toggles on the left.
First it will find all the available items, and deactivate the
existing category (if there was one). Afterwards it will toggle
into the new [in]active state, and will also trigger the activation
of items in the main text and update the count element in the main
text.
*/
function createCategoryToggleListeners() {
const tagSelectors = `${sidebarElementType}.clickable-tag`;
document.querySelectorAll(tagSelectors).forEach(el => {
el.addEventListener('click', function () {
/** Remove all existing active for siblings classes */
document.querySelectorAll(tagSelectors).forEach(item => {
if (item.dataset.tag !== el.dataset.tag) {
item.classList.remove('active');
}
});
el.classList.toggle('active');
const isActive = !!el.classList.contains('active');
toggleCategoryItem(`${el.dataset.tag}-item`, isActive);
adjustCount(el.dataset.tag, isActive);
});
});
}
/**
This code will basically activate the toggles on the left and add
the active class, and will do the same with the corresponding
items in the main text. in this case key highlights with have the
<span class="ti-key-high"> ... </span> tag surrounding it. Same
applies for the language tages, however with 'ti-lang-tone' and
'ti-lang-strat' attached to it.
*/
const toggleClasses = ['key-high', 'lang-tone', 'lang-strat', 'new-image'];
function activateToggle(toggleName) {
const indicatorEl = document.querySelector(`.${toggleName}-indicator`);
indicatorEl.classList.toggle('active');
document.querySelector('.main').classList.toggle('has-active-toggle');
document.querySelectorAll(`span.ti-${toggleName}`).forEach(el => {
el.classList.toggle(`ti-${toggleName}-active`)
});
}
function addToggleEventListeners() {
// create event listeners for the toggles in the sidebar
toggleClasses.forEach(item => {
const element = document.querySelector(`.toggle-${item}`);
if (element) {
element.addEventListener('click', function () {
activateToggle(item);
})
}
});
}
document.addEventListener('DOMContentLoaded', function () {
// create the items in the sidebar
createSideBarItems();
createCategoryToggleListeners();
addToggleEventListeners();
});
非常感谢您的帮助,我希望可以做点什么!
解决方案
推荐阅读
- webpack - 防止 HtmlWebpackPlugin 最小化生产中的换行符
- ionic-framework - 使用文件传输插件错误代码 3 加载视频时出错,异常:“Broken pipe”
- excel-formula - 如何结合 2 个规则并更改填充颜色
- ruby-on-rails - 当数据可用时生成进程并将数据发送到 STDIN
- django - 使用 Django rest_framework 检索与外键关联的所有对象的最佳方法是什么?
- pyspark - 以镶木地板格式写入时出现 PySpark 错误
- elasticsearch - Logback ElasticSearch appender 无法正确设置 url
- javascript - 更多 FileReader readAsText() 异步打字稿问题?
- optimization - 是否有最大化数量的简单算法或过程
- node.js - 在没有 express 的情况下测试 node.js RESTful API