如何将Class添加到肖像图像的照片/标题数组?

时间:2014-08-22 10:22:54

标签: javascript arrays gallery addclass

我有画廊工作,除了我不能为每个图像指定类,以便我可以更改垂直图像的比例。这是我到目前为止所做的。

代码:     

    var photos = new Array(), captions = new Array(); classes = new Array(); 
    var zero=0

    photos[0]="img/bg_array/img1.jpg";
    photos[1]="img/bg_array/img2.jpg";
    photos[2]="img/bg_array/img3.jpg";
    photos[3]="img/bg_array/img4.jpg";
    photos[4]="img/bg_array/img5.jpg";

    captions[0] = "one";
    captions[1] = "two";
    captions[2] = "three";
    captions[3] = "four";
    captions[4] = "five";

    classes[0] = "hor";
    classes[1] = "ver";
    classes[2] = "hor";
    classes[3] = "hor";
    classes[4] = "hor";

    function backward(){
        if (zero>0){
            window.status=''
            zero--
            document.images.photoarray.src=photos[zero]
            document.images.photoarray.addClass=classes[zero]
            document.descform.desc.innerHTML=captions[zero];
        }
        else { 
        window.status='Gallery Start'
        }
    }


    function forward(){
        if (zero<photos.length-1){
            zero++
            document.images.photoarray.src=photos[zero]
            document.images.photoarray.addClass=classes[zero]
            document.descform.desc.innerHTML=captions[zero];
        }
        else {
            window.status='Gallery End'
            document.descform.desc.innerHTML=captions[zero] + ' (End of Series)';
        }
    }


</script>

html:

<span>
    <img src="img/bg_array/img1.jpg" name="photoarray" oncontextmenu="return false;" class="hor">
    <form name="descform">
        <textarea name="desc" onFocus="this.blur()">one</textarea>
    </form>
    <div>
        <p id="back" onClick="backward()"></p>
        <p id="next" onClick="forward()"></p>
    </div>
</span>   

任何帮助都会很棒,提前谢谢。

2 个答案:

答案 0 :(得分:0)

试试这个。这可能会有所帮助。

this.setAttribute("class", classes[zero]);

答案 1 :(得分:0)

document.images不提供addClass方法。

你可以试试这个:

document.getElementsByName("photoarray")[0].setAttribute("class", classes[zero]);

或者给img标签一个id来获取

的访问权限
document.getElementById("xyz").setAttribute("class", classes[zero]);