在单击时会关闭扩展的div,在控制台中会单击时会记录数组对象。
如何更改我的函数以在第一次单击时获取数组对象,同时仍保持设置为第一次单击时要执行的功能?
_bindShowLess = function () {
var _showLess = _sectorPageStrengths.find('.view-all-sectors-btn.less');
_showLess.on('click', function () {
var sliders = [].slice.call(document.getElementsByClassName("sectorpage-strengths"));
sliders.forEach(function (element, index){
element.addEventListener("click", function(){
console.log("array" +index + "!");
});
});
$('html, body').animate({
scrollTop: _sectorPageStrengths.offset().top
}, 700);
});
};
我尝试了以下代码,但是现在它不能控制台日志我所在的div数组对象,而是所有对象。
_bindShowLess = function () {
var _showLess = _sectorPageStrengths.find('.view-all-sectors-btn.less');
_showLess.on('click', function () {
var sliders = [].slice.call(document.getElementsByClassName("sectorpage-strengths"));
sliders.forEach(function (element, index){
// element.addEventListener("click", function(){
console.log("array" +index + "!");
// });
});
// $('html, body').stop().animate({
// scrollTop: $(index).offset().top - 50
// }, 700);
$('html, body').animate({
scrollTop: _sectorPageStrengths.offset().top
}, 700);
});
};
init = function () {
var EachView = jQuery('.sectorpage-strengths');
EachView.each(function (index, element) {
_checkElemnt($(element));
_bindShowMore(element); // Individual container Fix Ben(2018)
_bindShowLess();
$(window).on('load', function () {
equalHeight();
});
});
$("#loadPDFComponentModal").on('hidden.bs.modal', function () {
$("#hiddenIframe").html("");
});
};
答案 0 :(得分:0)
我不确定我是否完全理解您的问题...这是您要找的吗?
_bindShowLess = function () {
var _showLess = _sectorPageStrengths.find('.view-all-sectors-btn.less');
_showLess.on('click', function () {
var sliders = [].slice.call(document.getElementsByClassName("sectorpage-strengths"));
var indices = [];
sliders.forEach(function (element, index){
indices.push("array" +index + "!");
});
console.table('Array indices!', indices);
$('html, body').animate({
scrollTop: _sectorPageStrengths.offset().top
}, 700);
});
};