如何将内容添加到JavaScript中动态创建的div中?
我有以下代码,向下滚动时显示“页面顶部”链接:如何将内容添加到JavaScript中动态创建的div中?,javascript,html,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
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
我不知道我做错了什么和/或什么?我想问一下如何将
添加到js创建的div中?动态创建元素时,请尝试以下操作:
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);
var smoothtop=新元素('div'{
'id':'smoothtop',
'class':'smoothtop',
“样式”:“位置:固定;显示:块;可见性:可见;缩放:1;不透明度:0;光标:指针;右:5px;下:5px;”,
“标题”:“,
“html”:“,
“事件”:{
mouseover:function(){fade_me(1);},
mouseout:function(){fade_me(0.7);},
单击:函数(){scroll.toTop();}
}
}).inject(document.body);
通过这种方式,您可以直接将元素注入DOM树,而无需稍后填充它。动态创建元素时,请尝试以下操作:
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);
var smoothtop=新元素('div'{
'id':'smoothtop',
'class':'smoothtop',
“样式”:“位置:固定;显示:块;可见性:可见;缩放:1;不透明度:0;光标:指针;右:5px;下:5px;”,
“标题”:“,
“html”:“,
“事件”:{
mouseover:function(){fade_me(1);},
mouseout:function(){fade_me(0.7);},
单击:函数(){scroll.toTop();}
}
}).inject(document.body);
通过这种方式,您可以直接将元素注入DOM树,而无需稍后填充它。根据您的示例,最简单的方法是在创建元素时使用
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);
var smoothtop=新元素('div'{
'id':'smoothtop',
'class':'smoothtop',
“样式”:“位置:固定;显示:块;可见性:可见;缩放:1;不透明度:0;光标:指针;右:5px;下:5px;”,
“标题”:“,
“html”:“”,//根据您的示例,最简单的方法是在创建元素时使用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);
var smoothtop=新元素('div'{
'id':'smoothtop',
'class':'smoothtop',
“样式”:“位置:固定;显示:块;可见性:可见;缩放:1;不透明度:0;光标:指针;右:5px;下:5px;”,
“标题”:“,
“html”:“,//您的代码在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);
您的代码位于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);