javascript - jquery 循环向上/向下移动

标签 javascript jquery

我正在尝试向上/向下移动 div 中的元素。一旦移动,item.position 就必须与前一个交换。下面是代码。有人可以建议如何将新分配的位置和关联的数据序列传递到数组中,以便它们可以传递到 Controller 并插入到数据库

<c:forEach var="item" items="${entry.value }" >

<ul>
<li> <div class="rows">
          <a id='${item.position}' data-seq='${attr.id}'  class="noDownloadFormat" href="/files/${item.value}" target="_blank"><c:out value="${item.title}" /></a>
         <div style="float:right;margin-top:-15px;">
             <input type="button" class="btn"  value="UP" />

          </div>
      </div>
</li>
<ul>
    </c:forEach>

Jquery:

 $(document).ready(function () {

   $(".btn1").click(function(){
var $current = $(this).closest('li')
var currentval =$(this).closest('li').children().find("a").prop("id");
var prevVal=$current.prev('li').children().find("a").prop("id");

    var $previous = $current.prev('li');
      if($previous.length !== 0){
        $current.insertBefore($previous);
    $(this).closest('li').children().find("a").prop("id",prevVal);
    $current.prev('li').children().find("a").prop("id")==currentval ;
     }
      return false;
    });



    });

谢谢

我修改了jquery,现在我可以上下移动但无法交换位置

最佳答案

我刚刚完成了向上按钮... Tweak it online .

<html>
<body>
<ul>
    <li>
        <div class="rows">
            <a id='position1' data-seq='id1'  class="noDownloadFormat" href="#" target="_blank">title1</a>
            <div style="float:right;margin-top:-15px;">
                <input type="button" class="btn" value="UP 1">
            </div>
        </div>
    </li>
    <li>
        <div class="rows">
            <a id='position2' data-seq='id2'  class="noDownloadFormat" href="#" target="_blank">title2</a>
            <div style="float:right;margin-top:-15px;">
                <input type="button" class="btn" value="UP 2">
            </div>
        </div>
    </li>
    <li>
        <div class="rows">
            <a id='position3' data-seq='id3'  class="noDownloadFormat" href="#" target="_blank">title3</a>
            <div style="float:right;margin-top:-15px;">
                <input type="button" class="btn" value="UP 3">
            </div>
        </div>
    </li>
</ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    var handler = function (e) {
        $li = $(e.target).parent().parent().parent();
        // Avoid delete element if it's at top of list.
        if ($li.prev().size() !== 0) {
            // Move element.
            $li.prev().before($li.remove());
            // Bind click handler to new button.
            $('.btn', $($li)).click(handler);
        }
    };
    $('.btn').on('click', handler);
</script>
</body>
</html>

关于javascript - jquery 循环向上/向下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29464958/

相关文章:

javascript - 使用 aspnet/signalr 通过 React 访问 Signal R

javascript - 单击子元素时忽略父 onClick 事件

javascript - Jquery Bootstrap 在点击时显示和隐藏跨度

javascript - 关闭所有弹出窗口

javascript - 如何在我的 Greasemonkey Javascript 脚本中使用 jQuery?

php - 自动调整缩略图在 DIV 中的位置(基于浏览器窗口宽度)?

javascript - 在 JavaScript 中计算 beta 累积概率密度的倒数

jquery - 循环遍历表行

android - Phonegap 拦截 jQueryMobile Ajax 调用

jQuery - 如何使用网络摄像头读取二维码?