讓我們的估計,我們已安排如下元素:通過類名稱獲取上一頁/上述元素(一個或多個)
<div><input class="elementToGet" value="1"></div> // st elementToGet
<div><span class"button">get prev elementToGet</span></div> // Nr1
<div><span class"button">get prev elementToGet</span></div> // Nr2
<div><span class"button">get prev elementToGet</span></div> // Nr3
<div><input class="elementToGet" value="2"></div> // nd elementToGet
<div><span class"button">get prev elementToGet</span></div> // Nr4
<div><span class"button">get prev elementToGet</span></div> // Nr5
<div><span class"button">get prev elementToGet</span></div> // Nr6
我怎樣才能得到每個跨度以前elementToGet
?
例如,從4開始編號的元素將得到第二個elementToGet
。
Javascript沒有框架需要。
function getPrev(className) {
var items = [], myPosts = document.getElementsByClassName(className);
for (var i = 0; i < myPosts.length; i++) {
// I have no ideas what to do next
items.push(myPosts[i]);
}
return items[0];
}
你讀過任何JavaScript文件或作出任何企圖? – thatidiotguy