1

<div class="move">I was moved to the parent.</div>在下面的代码中,当我单击 时,如何移动我单击的元素的父元素之后Add New

我一切都好,但我不能做我需要把它移到after()父母身上的部分。

你能帮忙吗?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Side bar poll thingy</title>
<script type="text/javascript" src="http://localhost/site/scripts/jQueryCore.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('.add').click(function() {
    $("#container").find('.flag, .edit, .delete').remove();
    $(this).parent("div").after(".move");

    });
});
</script>
</head>
<body>
<div id="container">
    <div class="h1" data-id="1">Teachers <span class="add" data-id="US01">Add New</span></div>
    <div class="h2" data-id="2">Who is your favorite Math teacher? <span class="add" data-id="US02">Add New</span></div>
        <br>
    <div class="h1" data-id="8">Restaurants <span class="add" data-id="US10">Add New</span></div>
    <div class="h2" data-id="9">Which is your favourtie restaurant in town? <span class="add" data-id="US20">Add New</span></div>

    <div class="move">I was moved to the parent.</div>
</div>
</body>
</html>
4

3 回答 3

2

.after()不接受选择器,目前您.move在所选元素之后插入字符串。您可以改用.insertAfter()方法:

$(".move").insertAfter(this.parentNode);

http://jsfiddle.net/RYzpG/

于 2013-09-08T08:13:29.917 回答
1

试试这个小提琴http://jsfiddle.net/QP3MZ/

$(document).ready(function () {
    $('.add').click(function () {
        $("#container").find('.flag, .edit, .delete').remove();
        var $move = $('#container .move');
        $(this).parent("div").after($move);
    });
});
于 2013-09-08T08:16:02.300 回答
1

这段代码应该可以工作。

$(document).ready(function() {
    $('.add').click(function(e) {
        e.preventDefault();
        $("#container").find('.flag, .edit, .delete').remove();
        $('.move').insertAfter($(this).parent());
        //Or (Note we have to pass a jQuery element inside after, if we pass string then it will copy the string instead)
        //$(this).parent('div').after($('.move'));
    });
});

这里还有一个jsfiddle。你的不工作的原因是你在 中传递一个字符串.after,jQuery 会将其视为 HTML 字符串。您必须传递一个 jQuery 元素或 DOM 元素。在这里看到它。

于 2013-09-08T08:28:50.250 回答