简体   繁体   English

如何在不使用 jquery 的情况下单击按钮时加载更多或显示更少的 div/cards

[英]How to load more or show less div/cards on button click without using jquery

I have a working code for this requirement in jquery which displays the specified number of cards/divs and loads more or loads less rows when I click on show more and show less buttons.我在 jquery 中有一个针对此要求的工作代码,当我单击“显示更多”和“显示更少”按钮时,它会显示指定数量的卡片/div 并加载更多或加载更少的行。

I need to use core javascript and I'm not able to achieve it in core javascript.我需要使用核心 javascript,但我无法在核心 javascript 中实现它。

Here is my fully working jquery code with demo:这是我完整工作的带有演示的 jquery 代码:

 var rowslength = 5; size_timelinediv = $("#timeline t").length; console.log(size_timelinediv)//prints 11 var x = 3; //number of cards to be displayed if (rowslength < 4) { $("#loadMoreprodDiv").hide(); } if (x == 3) { //alert(rowslength) $("#showlessprodDiv").hide(); } $("#timeline t:lt(" + x + ")").show(); $("#loadMoreprodDiv").click(function () { x = (x + 5 <= size_timelinediv) ? x + 5 : size_timelinediv; $("#timeline t:lt(" + x + ")").show(); $("#showlessprodDiv").show(); if (x == size_timelinediv) { $("#loadMoreprodDiv").hide(); } }); $("#showlessprodDiv").click(function () { x = (x - 5 <= 3) ? 3 : x - 5; $("#timeline t").not(":lt(" + x + ")").hide(); $("#loadMoreprodDiv").show(); $("#showlessprodDiv").show(); if (x == 3) { $("#showlessprodDiv").hide(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="timeline" id="timeline" > <t style="display:none"><div>Hi</div></t> <t style="display:none"><div>Hello</div></t> <t style="display:none"><div>How</div></t> <t style="display:none"><div>Are</div></t> <t style="display:none"><div>You</div></t> <t style="display:none"><div>I'm</div></t> <t style="display:none"><div>really</div></t> <t style="display:none"><div>Good</div></t> <t style="display:none"><div>x</div></t> <t style="display:none"><div>y</div></t> <t style="display:none"><div>z</div></t> </div> <button id="loadMoreprodDiv" class='loadMore'>View More Details</button><button id="showlessprodDiv" class='showLess'>View Less Details</button>

Now as per the requirement I need to convert this jquery code to core javascript :现在根据要求,我需要将此 jquery 代码转换为核心 javascript :

This is what I tried :这是我试过的:

size_timelinediv = document.getElementById("timeline").querySelectorAll("t").length;
console.log(size_timelinediv)//prints 11
var x = 3;//number of cards to be displayed

document.getElementById("timeline").querySelectorAll("t:lt(" + x + ")").style.display = "block"; //this doesn't work

But this doesn't work.但这不起作用。 It gives the following error :它给出了以下错误:

"Uncaught SyntaxError: Failed to execute 'querySelectorAll' on 'Element': 't:lt(3)' is not a valid selector."

Also I'm not sure how to achieve this line of code :另外我不确定如何实现这行代码:

 $("#timeline t").not(":lt(" + x + ")").hide();

in core javascript.在核心javascript中。 How do I achieve the same functionality using core javascript?如何使用核心 javascript 实现相同的功能?

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="timeline" id="timeline" > <t style="display:none"><div>Hi</div></t> <t style="display:none"><div>Hello</div></t> <t style="display:none"><div>How</div></t> <t style="display:none"><div>Are</div></t> <t style="display:none"><div>You</div></t> <t style="display:none"><div>I'm</div></t> <t style="display:none"><div>really</div></t> <t style="display:none"><div>Good</div></t> <t style="display:none"><div>x</div></t> <t style="display:none"><div>y</div></t> <t style="display:none"><div>z</div></t> </div> <button id="loadMoreprodDiv" class='loadMore'>View More Details</button><button id="showlessprodDiv" class='showLess' style="display:inline-block">View Less Details</button> <script> var rowslength = 5; size_timelinediv = document.querySelectorAll("#timeline t").length; console.log(size_timelinediv)//prints 11 var x = 3; //number of cards to be displayed if (rowslength < 4) { // $("#loadMoreprodDiv").hide(); document.querySelector("#loadMoreprodDiv").style.display = "none"; } if (x == 3) { //alert(rowslength) //$("#showlessprodDiv").hide(); document.querySelector("#showlessprodDiv").style.display = "none"; } showOnly(document.querySelectorAll("#timeline t"),x); document.getElementById("loadMoreprodDiv").onclick = function () { x = (x + 5 <= size_timelinediv) ? x + 5 : size_timelinediv; showOnly(document.querySelectorAll("#timeline t"),x); //$("#showlessprodDiv").show(); document.querySelector("#showlessprodDiv").style.display = "block"; if (x == size_timelinediv) { //$("#loadMoreprodDiv").hide(); document.querySelector("#loadMoreprodDiv").style.display = "none"; } } document.getElementById("showlessprodDiv").onclick = function () { x = (x - 5 <= 3) ? 3 : x - 5; showOnly(document.querySelectorAll("#timeline t"),x); document.querySelector("#loadMoreprodDiv").style.display = "block"; document.querySelector("#showlessprodDiv").style.display = "block"; //$("#loadMoreprodDiv").show(); //$("#showlessprodDiv").show(); if (x == 3) { document.querySelector("#showlessprodDiv").style.display = "none"; //$("#showlessprodDiv").hide(); } } function showOnly(nodeList, index) { for (i = 0; i < nodeList.length; i++ ) { nodeList[i].style.display = i<index ? "block": "none"; } } </script>

It seems, pure js does not recognise "lt"(less than) usage.看来,纯 js 无法识别“lt”(小于)用法。 So I wrote this helper function to show elements upto a specific index in a node list.所以我写了这个辅助函数来显示节点列表中特定索引的元素。

function showOnly(nodeList, index) {
 for (i = 0; i < nodeList.length; ++i ) {
   nodeList[i].style.display = i<index ? "block": "none";
 }
}

The following statement shows only the first 3 elements and hides the remaining from the list of elements returned by the selector.以下语句仅显示前 3 个元素,并从选择器返回的元素列表中隐藏其余元素。

showOnly(document.querySelectorAll("#timeline t"),3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM