Javascript 使用上下链接移动div Jquery
我有许多div与同一类,我需要找到一种方法来移动主题上下当我按下向上链接或向下链接:(Javascript 使用上下链接移动div Jquery,javascript,jquery,Javascript,Jquery,我有许多div与同一类,我需要找到一种方法来移动主题上下当我按下向上链接或向下链接:( 您需要在()之前查询,和()之后查询, 首先是一个模板: <div class="Container" id="Con1"> <div><a href="javascript: void(0);" class="up">Up</a><a href="javascript: void(0);" class="down">Down</a&
您需要在()之前查询
,
和()之后查询,
首先是一个模板:
<div class="Container" id="Con1">
<div><a href="javascript: void(0);" class="up">Up</a><a href="javascript: void(0);" class="down">Down</a></div>
Some textSome textSome textSome textSome textSome textSome textSome
textSome textSome textSome textSome textSome textSome textSome textSome textSome text
</div>
这是如何工作的,一个故障:
a
元素中添加了一个名为up
或down
的类。我还添加了一个运行JavaScript代码void
的href。它不返回任何内容,但会使a
元素作为链接。您需要将所有div
更改为此格式。正如注释所建议的那样,只使用向上
和向下
类,并将事件处理程序
(单击)附加到其中this
请求源元素。这指的是a
。a
是其容器div
的孙子。
div>div>a
。
因此,我需要调用parent('.Container')
。此函数将沿着DOM树向上运行,直到找到类名为Container
的第一个父级一个问题是您有多个具有相同ID(Con1)的div。此外,您的ID和类名的大小写应该较低。另一个问题是您的锚标记上需要
href
属性
您还需要一个包装器来包含要移动的部分
<div id="wrapper">
<div class="container" id="con1">
<div>
<a href="javascript:void(0); class="up">Up</a>
<a href="javascript:void(0); class="down">Down</a>
</div>
Some textSome textSome textSome textSome textSome textSome textSome
</div>
<div class="container" id="con2">
<div>
<a href="javascript:void(0); class="up">Up</a>
<a href="javascript:void(0); class="down">Down</a>
</div>
Some textSome textSome textSome textSome textSome textSome textSome
</div>
</div>
ID也应该是唯一的。您好,您是否编写了任何实际的jQuery,但它不起作用?@Ynhockey代码是submitted@MohamedSamy我已经完全更新了它。它现在反映了您的编码改进和一步一步的解释。使用它真的很有帮助。
<div class="Container" id="Con1">
<div><a href="javascript: void(0);" class="up">Up</a><a href="javascript: void(0);" class="down">Down</a></div>
Some textSome textSome textSome textSome textSome textSome textSome
textSome textSome textSome textSome textSome textSome textSome textSome textSome text
</div>
$(".up").click(function(e){
var parent = $(this).parent(".Container");
if (parent.prev().length > 0)
{
parent.prev().before(parent);
}
e.preventDefault();
});
$(".down").click(function(e){
var parent = $(this).parent(".Container");
if (parent.next().length > 0)
{
parent.next().after(parent );
}
e.preventDefault();
});
<div id="wrapper">
<div class="container" id="con1">
<div>
<a href="javascript:void(0); class="up">Up</a>
<a href="javascript:void(0); class="down">Down</a>
</div>
Some textSome textSome textSome textSome textSome textSome textSome
</div>
<div class="container" id="con2">
<div>
<a href="javascript:void(0); class="up">Up</a>
<a href="javascript:void(0); class="down">Down</a>
</div>
Some textSome textSome textSome textSome textSome textSome textSome
</div>
</div>
$('#wrapper').on('click','a', function(e) {
var $this = $(this);
var $container = $this.closest('.container');
var count = $('#wrapper').find('.container').length;
if ($this.hasClass('up')) {
// check to see if we're on the top level - if we are, do nothing
if (!$container.index() == 0) {
$('#wrapper').prepend($container);
}
}
else { // class id "down"
if (count != $container.index() - 1) {
$('#wrapper').append($container);
}
}
});