javascript - 使用 jquery 中的每个循环的 Next 和 Prev 按钮功能
问题描述
我的 div cl-content 中有多个项目。我在列表的每个项目中添加了上一个和下一个按钮。现在,当我单击这些按钮时,我尝试显示下一个和上一个 .cl-item,但我不知道如何使用 jQuery 编写此功能。
这是我的 HTML:
<div class="cl-content">
<div class="cl-item" itemscope="" itemtype="http://schema.org/Corporation" style="cursor: pointer;">
<div class="cl-item-illust">my Illustration</div>
<div class="cl-item-photo">
<img data-src="https://ready.cms-cxpm.com/var/comexposium/storage/images/media/ready-for-it-medias/icons/men/8838685-1-fre-FR/Men_contact_photo_list_ready_for_it_fre.jpg" src="https://ready.cms-cxpm.com/var/comexposium/storage/images/media/ready-for-it-medias/icons/men/8838685-1-fre-FR/Men_contact_photo_list_ready_for_it_fre.jpg" alt="Ready for IT" width="" height="" data-loaded="true">
</div>
<div class="cl-item-content">
<h3 class="cl-item-title" itemprop="name">John Doe</h3>
<p class="cl-item-subtitle">Responsable Stratégie Achat Logiciels, EDF</p>
<div itemprop="address" itemscope="" itemtype="http://schema.org/PostalAddress">
<p class="cl-item-address">My Adress</p>
</div>
</div>
</div>
</div>
这是我的js代码:
$(document).ready(function(){
if (window.location.href.indexOf("Comite-Editorial") > -1) {
let person = $(".cl-item")
let m
person.each(function(){
$(this).css('cursor', 'pointer')
$(this).click(function(){
m = $.trim($(this).find('h3').text())
l = $.trim($(this).find('p').text())
v = $.trim($(this).find('img').attr('src'))
let n = noty({
theme: 'relax',
closeWith: ['backdrop' , 'button'],
modal: true,
dismissQueue: false,
layout: 'center',
text: '<img src='+ v +'/><br><button class="prevButton"><</button><button class="nextButton">></button><br><h3>'+ m + '</h3><br><p>'+ l +'</p>',
animation: {
open: {height: 'toggle'},
close: {height: 'toggle'},
easing: 'swing',
speed: 200
}
})
})
})
}
})
解决方案
所以几个小时后,我做了这个,它不是很优雅,但它达到了我想要的效果:
$('body').click(function(event) {
$.noty.closeAll()
});
let person = $(".cl-item")
let m
let v
let l
let c
person.each(function(i, el){
$(this).css('cursor', 'pointer')
$(this).click(function(e){
e.stopPropagation()
m = $.trim($(this).find('h3').text())
l = $.trim($(this).find('p').text())
v = $.trim($(this).find('img').attr('src'))
c = $(this).html()
let n = noty({
theme: 'relax',
modal: true,
closeWith: ['backdrop', 'button'],
dismissQueue: false,
layout: 'center',
buttons: [
{
addClass: 'prevButton', text: '<', onClick: function(e, $noty) {
e.stopPropagation()
if (el.previousElementSibling != null) {
document.querySelector('div.mycl-item').innerHTML = el.previousElementSibling.innerHTML
el = el.previousElementSibling
}
}
},
{
addClass: 'nextButton', text: '>', onClick: function(e, $noty) {
e.stopPropagation()
if (el.nextElementSibling != null) {
document.querySelector('div.mycl-item').innerHTML = el.nextElementSibling.innerHTML
el = el.nextElementSibling
}
}
}
],
text: '<div onClick="$.noty.closeAll()" class="button_close">X</div>' + '<div onClick="event.stopPropagation();" class="mycl-item">'+ c +'</div>',
animation: {
open: {height: 'toggle'},
close: {height: 'toggle'},
easing: 'swing',
speed: 100
}
})
})
})
推荐阅读
- apache-spark - 当我们对数据帧执行操作时会创建 DAG 吗?
- java - 如何循环测验一定次数并增加生命,Java
- xamarin - 如何向我的 Xamarin.Forms 应用程序添加一个链接,以便它打开一个网页?
- mysql - 在 MySQL 中的每列上应用“DISTINCT”检索两列
- java - 如何解决我的 java 代码中的超时错误
- ios - 生成 IPA 时出现位码错误
- swift - 如何封装 Lazy Swift 函数的组合?
- c# - Google 地方信息自动填充不会填充地址
- c++ - 如何将字符串保存到动态字符数组中
- jmockit - 您如何在 JMockit 中模拟单例依赖项?