如何在Dom中显示描述文本图像3秒钟?
问题描述:
嗨我正在一个网站上工作。我需要帮助使文本出现在div中,它表示任何图像点击了它们的标题和大小。在DOM脚本中。谁能帮忙?没有innerhtml。如何在Dom中显示描述文本图像3秒钟?
感谢
答
使用纯DOM脚本,并像jQuery没有帮手框架,总得灰尘有些东西我还没有在一段时间使用!
这就是说这里雅去。必须在页面加载后放置。 (或删除最后一个“showCredit();”线,并将其放在你的身上onload。
注意你需要改变这一点,我只是把“源”放在文本中,其他属性和样式是给你。
function showCredit(){
//find all image tags we want
var elements = document.getElementsByTagName('img')
//iterate through them
for(var i=0; i<elements.length;i++){
//bind the onclick function to all image tags
elements[i].onclick=function(){
//create the new div
var el = document.createElement('div')
//alter this to be whatever text you want
var text = document.createTextNode('Source = '+this.getAttribute('src'));
//alter this if you're going to have more than one clickable div
el.id = 'test';
//add the text to the div
el.appendChild(text);
//add the new div after the image tag
this.parentNode.insertBefore(el, this.nextSibling);
//set a timer to find the element we've named "test" and remove it
window.setTimeout(function(){
var element = document.getElementById('test');
if(element){
element.parentNode.removeChild(element);
}
}, 4000);
}
}
}
//execute the function (bind all images)
showCredit();
+0
大代码,以完成这里所述OP请求是标题和大小的行:document.createTextNode( '源= '+ this.getAttribute(' SRC')+ ',title:'+ this.title +',size:'+ this.offsetWidth +'x'+ this.offsetHeight); – 2010-11-12 23:09:59
通常你会得到帮助这里固定的东西你不工作。你尝试过什么? – lincolnk 2010-11-12 19:44:36
功能showCredit(whichpic,X,Y){ \t VAR源= whichpic.getElementsByTagName (“img”); \t var title = source.getAttribue(“src”); \t var size = source.getAttribute(“width”,“height”); \t \t document.write(title + size); \t } \t \t 功能prepareTimers(){ \t \t \t \t的setTimeout(showCredit,4000); \t \t \t} \t \t \t的window.onload = prepareTimers – 2010-11-12 19:52:25