如何在Dom中显示描述文本图像3秒?

时间:2010-11-12 19:39:42

标签: javascript dom scripting

您好,我正在网站上工作。我需要帮助使文本出现在div中,它表示任何图像都会点击其标题和大小。在DOM脚本中。有人可以帮忙吗?没有innerhtml。

由于

1 个答案:

答案 0 :(得分:1)

使用纯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();