如何编写可以添加到更新占位符元素上的 alt 属性的 showpic 函数末尾的代码行?

How do I write a line of code that could be added to end of showpic function that updates an alt attribute on a placehold element?

我对编码和函数等很陌生。 我决定寻找在线试卷,以便从中学习问题,其中一个说:

Write a line of code that could be added to the end of the showpic function that updates the alt attribute on the placehold element.

然后是一个很长的编码是:

window.onload = prepareGallery;

    function prepareGallery() {
        var gallery, links, i;

        if (!document.getElementById) {
            return false;
        }

        if (!document.getElementsByTagName) {
            return false;
        }

        gallery = document.getElementById("images");
        links = gallery.getElementsByTagName("a");

        for(i = 0; i < links.length; i++) {
            links[i].onclick = function() {
                showPic(this);
                return false;
            }
        }
    }

    function showPic(pic) {
        var source, placehold, text, desc;

    source = pic.getAttribute("href");
    placehold = document.getElementById("place");
    placehold.setAttribute("src", source);

    text = pic.getAttribute("title");
    desc = document.getElementById("description");
    desc.firstChild.nodeValue = text;
}

会是

 pic.setAttribute("alt", "image");

鉴于 pic 参数是 <img></img> 标签的表示

setAttribute 有两个参数:第一个是标签名称(在您的例子中是 alt 标签),第二个参数是值。