我不确定是否允许您更改html,所以我只使用js,如果可以的话,将索引添加到html中,您可以将js减少一半
这是用“长”的方式做的,所以我希望你能理解步骤
$('.subpage_top_nav li').click(function(e) {
e.preventDefault();
/* find the element that needs to be moved */
var $toBeMoved = $('.need-to-go-back')
/* check if any1 actually needs to be moved */
if($toBeMoved) {
/* grab his init position */
var initPosition = $toBeMoved.attr('initial-position')
/* move the element there */
$('.subpage_top_nav li:eq(' + initPosition + ')').after($toBeMoved)
/* remove the signaling class */
$toBeMoved.removeClass('need-to-go-back')
/* remove the attr */
$toBeMoved.removeAttr('initial-position')
}
/* grab index */
var index = $(this).index()
/* save original index to know where it should be placed */
$(this).attr('initial-position', index)
/* add signaling class just to be easier to find it later */
$(this).addClass('need-to-go-back')
$(this).parent().prepend(this);
});