简体   繁体   English

在定位标记之前附加到ul

[英]Appending to a ul before an anchor tag

I am building a year list dynamically and I am truncating the list and displaying a "show more" and a "show less" link after the list of years. 我正在动态建立年份列表,并且删节列表并在年份列表之后显示“显示更多”和“显示更少”链接。 If I append the years to the ul , the years come after the "show more/less" links, and if I prepend the years, they show before the "show more/less" links but are in reverse order. 如果我append多年来以ul ,几年来经过“显示更多/更少”链接,如果我prepend这些年来,他们之前的“显示更多/更少”链接显示,而是以相反的顺序。

Is there a way for me to append the years to the ul but to append them, in chronological order, before the "show more/less" links? 我是否可以通过某种方式将年份附加到ul,然后按时间顺序在“显示更多/更少”链接之前附加年份?

 $(function() { $('.years-append').append('<li><a href="#">2014</a></li>'); $('.years-append').append('<li><a href="#">2015</a></li>'); $('.years-append').append('<li><a href="#">2016</a></li>'); $('.years-append').append('<li><a href="#">2017</a></li>'); $('.years-prepend').prepend('<li><a href="#">2014</a></li>'); $('.years-prepend').prepend('<li><a href="#">2015</a></li>'); $('.years-prepend').prepend('<li><a href="#">2016</a></li>'); $('.years-prepend').prepend('<li><a href="#">2017</a></li>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h2>Appending - Years are in correct order, but more/less links come BEFORE years</h2> <ul class="years-append"> <a class="btn-more" href="#"><span>+</span> More</a> <a class="btn-less" href="#"><span>-</span> Less</a> </ul> <h2>Prepending - more/less links come AFTER years, but years are in reverse order</h2> <ul class="years-prepend"> <!-- These links should come after the list of years, years should be in chronological order --> <a class="btn-more" href="#"><span>+</span> More</a> <a class="btn-less" href="#"><span>-</span> Less</a> </ul> 

Sorry if the wording is confusing, let me know if I need to clarify more. 抱歉,如果措辞令人困惑,请让我知道是否需要进一步说明。

Why not changing the HTML? 为什么不更改HTML? It's the easiest way to achieve what you want. 这是实现您想要的最简单的方法。 And your HTML is not valid by the way. 而且,您的HTML无效。 It's not valid to have <a> directly within a <ul> 直接在<ul>包含<a>是无效的

HTML: HTML:

<div>
  <ul class="years-append"></ul>
  <a class="btn-more" href="#"><span>+</span> More</a>
  <a class="btn-less" href="#"><span>-</span> Less</a>
</div>

JavaScript (untouched): JavaScript(未修改):

$(function() {
  $('.years-append').append('<li><a href="#">2014</a></li>');
  $('.years-append').append('<li><a href="#">2015</a></li>');  
  $('.years-append').append('<li><a href="#">2016</a></li>');  
  $('.years-append').append('<li><a href="#">2017</a></li>');
});

Explanation: Before you start scripting and styling you should try to bring the HTML in a logical order. 说明:开始编写脚本和样式之前,应尝试按逻辑顺序排列HTML。 In this case we mark the list and the buttons to belong together by setting them into a <div> . 在这种情况下,我们通过将列表和按钮设置为<div>标记它们属于在一起。 As the list should show up before the buttons, it's also in the HTML like that. 由于列表应该显示在按钮之前,因此它也在HTML中。

Fiddle: https://jsfiddle.net/8nz3pwtk/ 小提琴: https : //jsfiddle.net/8nz3pwtk/

append adds the element to the end of the parent element, while prepend adds it to the start. append将元素添加到父元素的末尾,而prepend将其添加到开始。 The show more and less links in your example are displaying above the year list because append is appending the years after those links. 在示例中,显示越来越少的链接显示在年份列表上方,因为append将这些链接之后附加年份。 The reason prepend is showing the years in reverse order is because you are prepending 2014, then prepending 2015, which would put it before 2014, etc. 优先显示年份倒序的原因是因为您先设置2014年,然后再设置2015年,然后再将其设置在2014年之前,依此类推。

You should place the Show more/less links outside of the list, and use append. 您应将显示更多/更少链接显示在列表之外,并使用附加。 This will put the years in order, and also fix the fact that you should not be placing a elements inside a ul . 这将使年顺序,还修复的事实,你不应该把a一个内部元素ul Source: http://w3c.github.io/html/grouping-content.html#the-ul-element 来源: http : //w3c.github.io/html/grouping-content.html#the-ul-element

 $(function() { $('.years-append').append('<li><a href="#">2014</a></li>'); $('.years-append').append('<li><a href="#">2015</a></li>'); $('.years-append').append('<li><a href="#">2016</a></li>'); $('.years-append').append('<li><a href="#">2017</a></li>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="years-append"> </ul> <a class="btn-more" href="#"><span>+</span> More</a> <a class="btn-less" href="#"><span>-</span> Less</a> 

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

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