используя чистый dom-сценарий и никакой вспомогательный фреймворк, такой как jquery, нужно стереть пыль с некоторых вещей, которыми я некоторое время не пользовался!
Это сказал, что ты иди.Должен быть размещен после загрузки страницы.(Или удалите последнюю строку "showCredit ();" и поместите ее в ваше тело при загрузке.
Обратите внимание, что вам нужно изменить это, я просто поместил "источник" в текст, другие атрибуты и стилизациюзависит от вас.
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();