Mootools注入克隆元素

时间:2013-02-27 13:56:49

标签: javascript mootools inject

我需要克隆一个元素,一旦页面调整大小,就会覆盖那里的当前元素。

我当前的代码不起作用如下:

var clone = false;
window.addEvent("domready",function(){
    clone = $$('.view').clone();
    /* Wait for images to be 100% loaded otherwise they might not have width/height */
    var lazyloader = new LazyLoad({
        onComplete: function(){
            setupWall();
            window.addEvent('resize:throttle(1000)', function(){
                /* Delete old viewport and add back with cloned version */
                var view = $$('.view');
                if(view){
                    wall = null;
                    view.dispose();
                }
                var cloneElement = $$('#wrapper').inject(clone);
                console.log(cloneElement);
                //setupWall();
            });
        }
    });
});

$$('#wrapper').inject(clone);运行时,我收到以下错误:

  

无法调用null

的方法'appendChild'

正如评论中所建议的那样,也是html。

<div id="wrapper">  
    <section class="view" id="wall">
        <div class="panel" id="panel1"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/701/310" /></div>
        <div class="panel" id="panel2"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/702/320" /></div>
        <div class="panel" id="panel3"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/703/330" /></div>
        <div class="panel" id="panel4"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/704/340" /></div>
        <div class="panel" id="panel5"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/705/350" /></div>
        <div class="panel" id="panel6"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/706/360" /></div>
        <div class="panel" id="panel7"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/707/370" /></div>
        <div class="panel" id="panel8"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/708/380" /></div>
        <div class="panel" id="panel9"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/709/390" /></div>
        <div class="panel" id="panel10"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/710/400" /></div>
        <div class="panel" id="panel11"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/711/410" /></div>
    </section>
</div>

1 个答案:

答案 0 :(得分:3)

$$()返回一个集合。当你真的想要一个元素时,你不应该克隆一个集合。

如果您想按ID获取单个元素,请使用$("id"); - 如果是选择器document.getElement('.class')(或任何node.getElement)。 $不像jquery,更像是document.getElementById()

以及element.inject(target) - 单个el,而不是集合 - vs target.adopt(element(s))

window.addEvent("domready",function(){
    var clone = document.getElement('.view').clone();
    /* Wait for images to be 100% loaded otherwise they might not have width/height */
    var lazyloader = new LazyLoad({
        onComplete: function(){
            setupWall();
            window.addEvent('resize:throttle(1000)', function(){
                /* Delete old viewport and add back with cloned version */
                var view = $$('.view');
                if(view.length){
                    wall = null;
                    view.destroy();
                }
                var cloneElement = $('wrapper').adopt(clone);
                console.log(cloneElement);
                //setupWall();
            });
        }
    });
});