简体   繁体   English

单击后,将元素设为其父列表的第一个子元素

[英]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小提琴,我想代码需要去以下地方:

JS Fiddle 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. 当然,请在所有四个处理程序中执行此操作。

Update: an updated fiddle 更新:更新的小提琴

在更新的小提琴中使用此处的 prepend方法

$('#id').parent().prepend($('#id'))

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.

相关问题 如何在jQuery中单击子元素而不是单击父元素? - How to make a click on a child element not be considered a click on it's parent in jQuery? 单击事件目标给出元素或其子元素,而不是父元素 - Click event target gives element or it's child, not parent element 使用子元素的 id 为父级绑定单击事件 - Bind click event for parent using child element's id 在React中,如何在父元素内的子元素中测试click事件 - In React, how to test the click event in child that's inside a parent element 子元素live。(&#39;click&#39;)从不运行,而是父div的Click始终运行 - Child element live.('click') never runs instead Parent div's Click always runs 如何在单击或双击父元素时扩展树形列表的子节点? - How to Expand child nodes of tree list on click or dbl click on parent element? 鼠标单击父元素,但防止其子元素触发事件 - javascript - Mouse Click on parent element , but prevent it's child element to fire event 如何使父元素成为子元素的大小? - How to make parent element to be the size of the child element? 点击事件的父/子元素绑定 - Parent / Child element bindings for click event 单击子元素时停止触发父事件 - Stop parent event firing on child element click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM