如何在JavaScript中将内容添加到动态创建的div中?

时间:2013-08-28 13:59:34

标签: javascript html

我有这段代码,向下滚动时会显示“页面顶部”链接:

window.addEvent('load', function() {
                new JCaption('img.caption');
            });
function fade_me(num){
    var smoothtop=document.id('smoothtop');
    if(smoothtop){smoothtop.fade(window.getScrollTop()<250?0:num);}
}
window.addEvent('domready',function(){
    var scroll=new Fx.Scroll(window,{
        'duration': 500,
        'transition':   Fx.Transitions.Bounce.easeInOut,
        'wait':     false
    });
    var smoothtop=new Element('div',{
        'id':       'smoothtop',
        'class':    'smoothtop',
        'style':    'position:fixed; display:block; visibility:visible; zoom:1; opacity:0; cursor:pointer; right:5px; bottom:5px;',
        'title':    '',
        'html':     '',
        'events':{
            mouseover: function(){fade_me(1);},
            mouseout: function(){fade_me(0.7);},
            click: function(){scroll.toTop();}
        }
    }).inject(document.body);


    document.id('smoothtop').setStyle('opacity','0');
});
window.addEvent('scroll',function(){fade_me(0.7);});

//this is what I added
var ii = document.getElementById('smoothtop');
ii.childNodes[0].nodeValue = '<i class="icon icon-chevron-up"></i>';
//these two lines

正如您所看到的,代码生成了一个带有id smoothtop的div。它有一个向上箭头图片,表示页面顶部。相反,我想使用

使用BootStrap的glyphicon
<i class="icon icon-chevron-up"></i>

我尝试将此内容添加到div smoothtop中。当我用FireBug检查代码时,它说:

TypeError: ii is null

var ii = document.getElementById('smoothtop');

我无法确定在哪里和/或我做错了什么?我想问一下如何将<i></i>添加到由js创建的div中?

3 个答案:

答案 0 :(得分:3)

根据您的示例,最简单的方法是在创建元素时使用html属性:

var smoothtop=new Element('div',{
    'id':       'smoothtop',
    'class':    'smoothtop',
    'style':    'position:fixed; display:block; visibility:visible; zoom:1; opacity:0; cursor:pointer; right:5px; bottom:5px;',
    'title':    '',
    'html':     '<i class="icon icon-chevron-up"></i>', // <-- right here
    'events':{
        mouseover: function(){fade_me(1);},
        mouseout: function(){fade_me(0.7);},
        click: function(){scroll.toTop();}
    }
}).inject(document.body);

答案 1 :(得分:2)

您的代码在domready处理程序之外,但创建该元素的代码放在domready处理程序中。因此,var ii = document.getElementById('smoothtop');在元素被创建之前执行。

只需将代码放在domready处理程序的末尾,ii引用就不会是null

此外,我强烈建议您使用库的帮助程序类和函数来操作DOM,因为您的库可能会处理跨浏览器问题和其他烦恼。

然而,在vanilla JS中,您可以这样做:

var smoothtopEl = document.getElementById('smoothtop'),
    i = document.createElement('i');

i.className = 'icon icon-chevron-up';
smoothtopEl.appendChild(i);

答案 2 :(得分:1)

动态创建元素时,请尝试以下操作:

var smoothtop=new Element('div',{
    'id':       'smoothtop',
    'class':    'smoothtop',
    'style':    'position:fixed; display:block; visibility:visible; zoom:1; opacity:0; cursor:pointer; right:5px; bottom:5px;',
    'title':    '',
    'html':     '<i class="icon icon-chevron-up"></i>',
    'events':{
        mouseover: function(){fade_me(1);},
        mouseout: function(){fade_me(0.7);},
        click: function(){scroll.toTop();}
    }
}).inject(document.body);

通过这种方式,您可以将元素直接注入到DOM树中,而无需在以后填充它。