本不好意思写出来,结果看到51js有人留言向参考,于是贴出来算了。
//button class
var button = function(buttonId,buttonValue,index){
var bt = document.createElement("input");
bt.setAttribute("type","button");
bt.setAttribute("id", buttonId);
bt.setAttribute("value",buttonValue);
bt.attachEvent("onclick", function(){controlImg(event, index)});
return bt;
}
//image control
function controlImg(event,index)
{
if (index == 1)
{
m_imgLocation = parseInt(document.getElementById("imgLocation").value);
if (m_imgLocation <= minIndex)
m_imgLocation = maxIndex;
else
m_imgLocation-=1;
var img = document.getElementById("img1");
if (null != img)
{
var divMain = document.getElementById("main");
divMain.removeChild(img);
}
var img = document.createElement("img");
img.setAttribute("id","img1");
img.className = "photo";
img.src ="pic_" + m_imgLocation + ".jpg";
divMain.appendChild(img);
document.getElementById("imgLocation").value = m_imgLocation;
}
else
{
m_imgLocation = parseInt(document.getElementById("imgLocation").value);
if (m_imgLocation >= maxIndex)
m_imgLocation = minIndex;
else
m_imgLocation+=1;
var img = document.getElementById("img1");
if (null != img)
{
var divMain = document.getElementById("main");
divMain.removeChild(img);
}
var img = document.createElement("img");
img.setAttribute("id","img1");
img.className = "photo";
img.src = "pic_" + m_imgLocation + ".jpg";
divMain.appendChild(img);
document.getElementById("imgLocation").value = m_imgLocation;
}
}
function createImage()
{
var img = document.createElement("img");
img.setAttribute("id","img1");
img.className = "photo";
img.src = "pic_0.jpg";
divTable.appendChild(img);
var btnLast = new button("Last","<<","1");
btnLast.className = "LastImg";
var btnNext = new button("Next",">>","2");
btnNext.className = "NextImg";
var table = document.createElement("table");
var tbody = document.createElement("tbody");
table.appendChild(tbody);
tbody.insertRow(0);
tbody.rows[0].insertCell(0);
tbody.rows[0].cells[0].appendChild(btnLast);
tbody.rows[0].insertCell(1);
tbody.rows[0].cells[1].appendChild(btnNext);
divTable.appendChild(table);
}