[英]Make element the first child of it's parent list, on click
I have a ul list with 4 list items in it. 我有一个带有4个列表项的ul列表。 The list items have IDs named #one, #two, #three and #four:
列表项的ID为#one,#two,#3和#four:
<ul class="home_slider">
<li id="one">one</li>
<li id="two">two</li>
<li id="three">three</li>
<li id="four">four</li>
</ul>
There is also another ul list with 4 corresponding list items: 还有另一个ul列表,其中包含4个相应的列表项:
<ul class="btn_wrap">
<li class="btn" id="btn-01"></li>
<li class="btn" id="btn-02"></li>
<li class="btn" id="btn-03"></li>
<li class="btn" id="btn-04"></li>
</ul>
The four list items in the second ul list act as buttons using jQuery click. 第二个ul列表中的四个列表项使用jQuery click充当按钮。
The buttons correspond to the first ul list's items, for example #btn-01 manipulates #one. 这些按钮与第一个ul列表的项目相对应,例如#btn-01操作#one。
The question 问题
I want to make it so when you click on #btn-01, it makes #one become the first-child of its parent ul "home_slider". 我想这样做,当您单击#btn-01时,它使#one成为其父ul“ home_slider”的第一个孩子。
Same logic applies to the other buttons, so when you click #btn-02 it would make #two become the first child of "home_slider". 相同的逻辑适用于其他按钮, 因此,当您单击#btn-02时,它将使#two成为“ home_slider”的第一个子代。
Here is a JS Fiddle with comments where I imagine the code needs to go: 这是一个带有注释的JS小提琴,我想代码需要去以下地方:
There is slightly more going on in the fiddle, such as classes being added on click, but for simplicity's sake, I left that out as it doesn't really pertain to the question. 小提琴中还有更多的事情要做,例如单击添加类,但是为了简单起见,我将其省略,因为它实际上与问题无关。
Since you are using jQuery, you can use remove()
and prependTo()
. 由于您使用的是jQuery,因此可以使用
remove()
和prependTo()
。
For instance, this will remove #four
and prepend it to the beginning of .home_slider
: 例如,这将删除
#four
并将其添加到.home_slider
的开头:
$('#four').remove().prependTo('.home_slider');
Of course, do this in all four of your handlers. 当然,请在所有四个处理程序中执行此操作。
There are a few things that will help you here, I altered your fiddle as shown: 有几件事可以帮助您,在这里我更改了您的小提琴 ,如下所示:
HTML HTML
<ul class="home_slider">
<li data-id="btn-01" id="one">one</li>
<li data-id="btn-02" id="two">two</li>
<li data-id="btn-03" id="three">three</li>
<li data-id="btn-04" id="four">four</li>
</ul>
<ul class="btn_wrap">
<li class="btn" id="btn-01"></li>
<li class="btn" id="btn-02"></li>
<li class="btn" id="btn-03"></li>
<li class="btn" id="btn-04"></li>
</ul>
JS JS
jQuery('.btn').click(function() {
var target = '[data-id="' + $(this).attr('id') + '"]';
$('.btn').removeClass('btn-selected');
$(this).addClass('btn-selected');
$(target).prependTo('.home_slider');
$('.home_slider > li').removeClass('active');
$(target).addClass('active');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.