使用javascript进行动态HTML分页

时间:2019-09-11 15:36:46

标签: javascript pagination html-lists code-generation dynamic-html

我有以下分页控制 The initial state

index.html(初始状态)中的代码:

<ul id="page_paging">
    <li><a href="#previous_section">&lt;</a></li>
    <li id= "active_page" class="active"><span>1</span></li>
    <li><a href="#previous_section">2</a></li>
    <li><a href="#previous_section">3</a></li>
    <li><a href="#previous_section">4</a></li>
    <li><a href="#previous_section">5</a></li>
    <li><a href="#previous_section">&gt;</a></li>
</ul>

在javascript中,我附加了事件监听器:

document.getElementById("page_paging").addEventListener("click", LoadPages);

然后在功能中,我根据用户的点击生成页面列表

function generateUL(minPage, maxPage, newActivePage){
    var innerHtml = "<li><a href=\"#previous_section\">&lt;</a></li>";

    for (var pageIdx = minPage; pageIdx <= maxPage; pageIdx++) 
    {
        if( pageIdx == newActivePage) { 
            innerHtml += "<li id= \"active_page\" class=\"active\"><span>" + pageIdx + "</span></li>";
        }
        else{
            innerHtml += "<li><a href=\"#previous_section\">" + pageIdx + "</li>";
        }
    }
    innerHtml += "<li><a href=\"#previous_section\">&gt;</a></li>" ;  

    return innerHtml;
}

尽管返回的html看起来与初始状态“原始状态”完全相同-控件松开了圆圈之间的空格

结果html:

<ul id="page_paging">
    <li><a href="#previous_section">&lt;</a></li>
    <li id="active_page" class="active"><span>1</span></li>
    <li><a href="#previous_section">2</a></li>
    <li><a href="#previous_section">3</a></li>
    <li><a href="#previous_section">4</a></li>
    <li><a href="#previous_section">5</a></li>
    <li><a href="#previous_section">&gt;</a></li>
</ul>

以及控件的外观 enter image description here

有人知道为什么会发生这种情况以及如何解决吗?

3 个答案:

答案 0 :(得分:0)

结果HTML具有不同的ID(“ episode_paging”),因此CSS可能有所不同。 当涉及到代码本身而不是“ #previous_section”时,我将使用“数据页”或类似方法,在普通的JS样板下面:

<ul id="episode_paging">
    <li><a data-prev="1" href="#">&lt;</a></li>
    <li id="active_page" class="active"><span>1</span></li>
    <li><a href="#" data-page="2">2</a></li>
    <li><a href="#" data-page="3">3</a></li>
    <li><a href="#" data-page="4">4</a></li>
    <li><a href="#" data-page="5">5</a></li>
    <li><a data-next="1" href="#">&gt;</a></li>
</ul>
<script>
    var inputElem = document.getElementById("episode_paging").getElementsByTagName("a");
    for (var i = 0; i < inputElem.length; i++) {

        inputElem[i].addEventListener('click', function () {
            if (this.getAttribute("data-page"))
                alert(this.getAttribute("data-page"));
        }, false);
    }
</script>

答案 1 :(得分:0)

只需使用

之类的CSS
.page_paging li {
  padding-left: 2px !important;
  padding-right: 2px !important;
}

对于您的功能,我会这样做

function generateUL(minPage, maxPage, newActivePage){
    var innerHtml = "<li><a href=\"#previous_section\">&lt;</a></li>";
    for (var pageIdx = minPage; pageIdx <= maxPage; pageIdx++) 
    {

        if( pageIdx == newActivePage) { 
            var liElement = "<li id= \"active_page\" class=\"active\"><span>" + pageIdx + "</span></li>";
        }
        else{
            var liElement = "<li><a href=\"#previous_section\" >" + pageIdx + "</li>";
        }
        innerHtml += liElement;
    }

    innerHtml += "<li><a href=\"#previous_section\">&gt;</a></li>" ;  

    return innerHtml;
}

答案 2 :(得分:0)

示例CSS:

#episode_paging li {
    padding: 0 2px !important;
}

#episode_paging li a {
    padding: 0 2px !important;
}