1

我在我的项目中使用PagePiling ,特别是添加菜单的选项。

根据文档,您可以简单地添加 html:

<ul id="myMenu">
    <li data-menuanchor="firstPage" class="active"><a href="#firstPage">First section</a></li>
    <li data-menuanchor="secondPage"><a href="#secondPage">Second section</a></li>
    <li data-menuanchor="thirdPage"><a href="#thirdPage">Third section</a></li>
    <li data-menuanchor="fourthPage"><a href="#fourthPage">Fourth section</a></li>
</ul>

并在 js 中:

$('#pagepiling').pagepiling({
    anchors: ['firstPage', 'secondPage', 'thirdPage', 'fourthPage', 'lastPage'],
    menu: '#myMenu'
});

但是,我想添加动态菜单元素,例如:

<ul id="myMenu">
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <li data-menuanchor="<?php sanitize_title(the_title()); ?>"><a href="#<?php sanitize_title(the_title()); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
</ul>

我如何也更新js中的选项?

4

1 回答 1

0

我认为您可以通过 JS 获取 menuanchor 数组。

var myAnchors = [];

$( "#myMenu li" ).each(function( index ) {
  myAnchors.push($(this).attr("data-menuanchor"));
});

console.log(myAnchors);

$('#pagepiling').pagepiling({
    anchors: myAnchors,
    menu: '#myMenu'
});
于 2016-09-26T22:57:56.333 回答