Ajax jQuery手风琴在页面上显示前5个元素

时间:2020-07-01 21:53:44

标签: javascript jquery ajax jquery-ui-accordion

我想做的是Ajax页面中排名前五的元素。通过ajax加载现有内容后,我很难将其保留为手风琴格式。是否可以使用.load()来拉前五名?我该怎么解决这个问题?

我想从以下页面进行ajax的页面:

<div id="main" class="accordians">
    <h3>Collapsible Group Item #1</h3>
    <div>
        <p>This is section 1. Place your content here in paragraphs or use div elements etc.</p>
    </div>
    <h3>Collapsible Group Item #2</h3>
    <div>
        <p>This is section 2. Place your content here in paragraphs or use div elements etc.</p>
    </div>
    <h3>Collapsible Group Item #3</h3>
    <div>
        <p>This is section 3. Place your content here in paragraphs or use div elements etc.</p>
    </div>
    <h3>Collapsible Group Item #4</h3>
    <div>
        <p>This is section 4. Place your content here in paragraphs or use div elements etc.</p>
    </div>
    <h3>Collapsible Group Item #5</h3>
    <div>
        <p>This is section 5. Place your content here in paragraphs or use div elements etc.</p>
    </div>
    <h3>Collapsible Group Item #6</h3>
    <div>
        <p>This is section 6. Place your content here in paragraphs or use div elements etc.</p>
    </div>
</div>

Accordion Looks like the following

第二页将通过Ajax拉动手风琴:

<div id="load-top-five">#load top five accordions into here.</div>
<script>
   // would like to combine the functionality to only return the top five.
   $('#load-top-five').load('https://www.example.com #main'); // this loads the page but doesn't keep it in the correct format.
   $('#load-top-five').find('#main:lt(5)').each(function(){...} // I would like to do some logic like this to only render the top 5.
<script>

2 个答案:

答案 0 :(得分:1)

您可以通过这种方式完成

在html文件中

<div id="main" class="accordians">
    <div class="card-acordeon">
        <div id="collapse1"> 
            <h3 class="title">header 1</h3>
        </div>
        <div class="acordeon" id="acordeon1">
            <p>This is section 1. Place your content here in paragraphs or use div elements etc.</p>
        </div>
    </div>
    <div class="card-acordeon">
        <div id="collapse2"> 
            <h3 class="title">header 2</h3>
        </div>
        <div class="acordeon" id="acordeon2">
            <p>This is section 2. Place your content here in paragraphs or use div elements etc.</p>
        </div>
    </div>
    <div class="card-acordeon">
        <div id="collapse3"> 
            <h3 class="title">header 3</h3>
        </div>
        <div class="acordeon" id="acordeon3">
            <p>This is section 3. Place your content here in paragraphs or use div elements etc.</p>
        </div>
    </div>
    <div class="card-acordeon">
        <div id="collapse4"> 
            <h3 class="title">header 4</h3>
        </div>
        <div class="acordeon" id="acordeon4">
            <p>This is section 4. Place your content here in paragraphs or use div elements etc.</p>
        </div>
    </div>
    <div class="card-acordeon">
        <div id="collapse5"> 
            <h3 class="title">header 5</h3>
        </div>
        <div class="acordeon" id="acordeon5">
            <p>This is section 5. Place your content here in paragraphs or use div elements etc.</p>
        </div>
    </div>
    <div class="card-acordeon">
        <div id="collapse6"> 
            <h3 class="title">header 6</h3>
        </div>
        <div class="acordeon" id="acordeon6">
            <p>This is section 6. Place your content here in paragraphs or use div elements etc.</p>
        </div>
    </div>
    <div class="card-acordeon">
        <div id="collapse7"> 
            <h3 class="title">header 7</h3>
        </div>
        <div class="acordeon" id="acordeon7">
            <p>This is section 7. Place your content here in paragraphs or use div elements etc.</p>
        </div>
    </div>
    <div class="card-acordeon">
        <div id="collapse8"> 
            <h3 class="title">header 8</h3>
        </div>
        <div class="acordeon" id="acordeon8">
            <p>This is section 8. Place your content here in paragraphs or use div elements etc.</p>
        </div>
    </div>
</div>

在css文件中

*{
            padding: 0;
            margin: 0;
        }

        .accordians{
            height: auto;
            width: 80%;
            padding-top: 4%;
            margin: auto;
        }

        .card-acordeon{
            width: 100%;
            height: auto;
            cursor: pointer;
            padding-left: 2%;
            padding-top: 1%;
            padding-bottom: 1%;
            border-bottom: 1px solid rgb(121, 121, 121);
            background-color: gray;
        }

        .card-acordeon div:nth-child(2){
            display: none;
        }

        .card-acordeon:hover{
            opacity: 0.9;
        }

        .title{
            color: white;
            font-size: 20px;
            font-weight: bolder;
            height: 100%;
            width: 100%;
        }
        .acordeon{
            margin-top: 2%;
            padding-left: 1%;
            padding-right: 1%;
        }

在js文件中

$(document).ready(function () {
        console.log("hay "+$(".card-acordeon").length+" div con la clase card-acordeon");
        for(var i=0; i<$(".card-acordeon").length; i++){
            let pos=i+1;
            if(i<5){
                $("#acordeon"+pos).css('display','block');
            }
            $("#collapse"+pos).click(Collapsediv);
        }
    });

    var idbefor=0;
    var id=0;

    function Collapsediv(){
        id=this.id.substring(this.id.length-1,this.id.length);
        $("#acordeon"+id).css('display','block');
        if(idbefor!=id){
            $("#acordeon"+idbefor).css('display','none');
        }
        else{
            $("#acordeon"+id).css('display','block');
        }
        idbefor=this.id.substring(this.id.length-1,this.id.length);
    }

答案 1 :(得分:0)

首先,您必须将元素加载到页面上。手风琴只有在数据加载完成后才能格式化。

$(function() {
    $('#load-into-new-accordion').load('https://www.someurl.com #accordion_data');
});

// create a function for the accordion.
function loadAccordionFormating() {
  $('#load-into-new-accordion').accordion({});
}

// after the data is done loading apply the formatting.
window.onload = function() {
   loadAccordionFormating();
};

要隐藏元素,可以使用ui-id-1,ui-id-2等,您只需创建一个简单的for循环即可。

for(var i=1;i<15; i++) {
    $(`ui-id-${i}`).hide();
}

此外,由于这很慢,因此您还可以默认使用CSS隐藏元素,并使用与上述相同的方法显示它们。