How to use original JAVASCRIPT to only display part of the five LIs and display the rest when clicked?
世界只因有你
世界只因有你 2017-05-19 10:18:35
0
5
417

code show as below:

The above is the HTML structure. How to use JAVASCRIPT (not JQUERY) to only display 5 links and hide the rest. You need to click on the picture or text to display it, and then click to hide it. How can I achieve this function?

Currently displayed:

I am link 1
I am link 2
I am link 3
I am link 4
I am link 5
...
I am link 10

The effect you want to display:

I am link 1
I am link 2
I am link 3
I am link 4
I am link 5
︿ //Click here to expand the remaining "I am link" 6-10", then click to hide "I am the link 6-10".

世界只因有你
世界只因有你

reply all (5)
伊谢尔伦

Let’s talk about a stupid way, asynchronous request. Load only what you need first, click the button to request the rest, and dynamically load it into the page. Add a class to the newly added li for click hiding.

 

$("#show").on('click',function(){ $(".testbox>ul>li").each(function(){ if($(this).find("a").attr("class") == "hide"){ $(this).find("a").removeClass("hide").addClass("show"); }else if($(this).find("a").attr("class") == "show"){ $(this).find("a").removeClass("show").addClass("hide"); } }); }) });

The code is rather ugly, please forgive me (using jquery api)

    我想大声告诉你

    After reading the above answers, I don’t think they are my thoughts. Let’s talk about mine here

      let listContainer = document.querySelector('#list-container') let docfrag = document.createDocumentFragment() function loadNode (n) { for (let i = 0; i < n; i++) { let snippest = document.createElement('li') snippest.innerHTML = '' + i + '' docfrag.appendChild(snippest) } listContainer.appendChild(docfrag) }

      Every time you call loadNode(), pass in the number of li to be generated as a parameter, and it can be generated dynamically

        左手右手慢动作

        I can’t quite understand what you mean

        css:
        Set a reserved classname for li, such as .hide {display:none}
        Now add hide to the following li classes

        js:
        Use the click event to trigger to determine whether there is a hide, and then delete the class or add the class according to the situation.

        Probably this is the route.

          为情所困

            洪涛

            The idea is to let ul overflow:hidden and then change the height.

            Wrote a draft https://jsfiddle.net/straybug...

              Latest Downloads
              More>
              Web Effects
              Website Source Code
              Website Materials
              Front End Template
              About us Disclaimer Sitemap
              php.cn:Public welfare online PHP training,Help PHP learners grow quickly!