为什么前置功能不起作用?

时间:2014-03-04 04:33:56

标签: javascript jquery

我正在制作一个演示。它不会预备第一个div数据div第二个div数据(当用户滚动到顶部.it不是预备和数据时)。

你能解释为什么没有发生吗?

http://jsfiddle.net/G6jJS/4/

$("#fullContainer").scroll(function(){
        if($(this).scrollTop() === 0){
            alert("----")

            $("#firstcontainter").html(secondData);
            $("#secondcontainter").prependTo("#firstcontainter");
        }
});

2 个答案:

答案 0 :(得分:2)

因为#secondcontainer#firstcontainer的孩子,所以当您致电...

$("#firstcontainter").html(secondData);

您正在完全删除#secondcontainer。试试这个......

var second = $('#secondcontainer').detach();
$("#firstcontainer").html(secondData).prepend(second);

你还有一个未公开的<div>和其他一些错别字。所有修正都在这里 - http://jsfiddle.net/G6jJS/9/

答案 1 :(得分:0)

此代码后没有#secondcontainer

$("#firstcontainter").html(secondData);

要解决此问题,只需存储内容并在以后添加前缀:

var second = $('#secondcontainer')[0].outerHTML;
$("#firstcontainer").html(secondData).prepend(second);