简体   繁体   English

通过 Jquery 将无序列表转换为下拉列表:当前页码不显示

[英]Converting an unordered list to drop-down through Jquery: current page number does not display

I am trying to convert an unordered list to a drop-down list through Jquery in Wordpress.我正在尝试通过 Wordpress 中的 Jquery 将无序列表转换为下拉列表。

I've managed to turn ul li s to to a select list, however there is a problem: Current page number does not show up in the drop-down list.我已经设法将ul li s 转换为select列表,但是有一个问题:当前页码未显示在下拉列表中。

I understand that the root of the problem is that the selected list is not a link and due to the given plugin structure I am not able to convert current page number to a link.我知道问题的根源是所选列表不是链接,并且由于给定的插件结构,我无法将当前页码转换为链接。

How can I solve this issue?我该如何解决这个问题? Thanks.谢谢。

 jQuery(document).ready(function($) { $('ul.pagination').each(function() { var list = $(this), select = $(document.createElement('select')).insertBefore($(this).hide()).change(function() { window.open($(this).val(), '_self') }); $('>li a', this).each(function() { var option = $(document.createElement('option')).appendTo(select).val(this.href).html($(this).html()); if ($(this).attr('class') === 'page-numbers current') { option.attr('selected', 'selected'); } }); list.remove(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="loopage_pg" class="pagination-wrap"> <ul class="pagination"> <li><a class="page-numbers" href="http://www.example.com/?pg=1">1</a></li> <li class="active "><span aria-current="page" class="page-numbers current">2</span></li> <li><a class="page-numbers" href="http://www.example.com/?pg=3">3</a></li> <li><a class="page-numbers" href="http://www.example.com/?pg=4">4</a></li> <li><a class="page-numbers" href="http://www.example.com/?pg=5">5</a></li> </ul> </div>

The current page is stored in a span .当前页面存储在span中。 However the second query only looks for nested anchor tags in list items.但是,第二个查询仅在列表项中查找嵌套的锚标记。 Extend your query with ', > li span' to also look for a span tag inside the list item.使用', > li span'扩展您的查询,以在列表项中查找 span 标签。

When checking for a class you could also use the hasClass() method.检查 class 时,您还可以使用hasClass()方法。

 jQuery(document).ready(function($) { $('ul.pagination').each(function() { var list = $(this), select = $(document.createElement('select')).insertBefore($(this).hide()).change(function() { window.open($(this).val(), '_self') }); $('> li a, > li span', this).each(function() { var option = $(document.createElement('option')).appendTo(select).val(this.href).html($(this).html()); if ($(this).hasClass('current')) { option.attr('selected', 'selected'); } }); list.remove(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="loopage_pg" class="pagination-wrap"> <ul class="pagination"> <li><a class="page-numbers" href="http://www.example.com/?pg=1">1</a></li> <li class="active "><span aria-current="page" class="page-numbers current">2</span></li> <li><a class="page-numbers" href="http://www.example.com/?pg=3">3</a></li> <li><a class="page-numbers" href="http://www.example.com/?pg=4">4</a></li> <li><a class="page-numbers" href="http://www.example.com/?pg=5">5</a></li> </ul> </div>

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

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