在以下代碼:推理碼:+ i +和+ X +
for (i=0; i<itemsinlist.length; i++) {
var rating = document.createElement('div');
itemsinlist[i].appendChild(rating);
rating.className = "rating";
rating.id = "thumbnails" +i;
for (x=0; x<4; x++) {
star = document.createElement('span');
star.innerHTML = "★";
star.className = "star";
star.setAttribute("onclick", "ratingsSet("+i+","+x+");");
rating.appendChild(star);
} //createratingsstars
我努力使所述第二參數的感star.setAttribute()
,在該行:
star.setAttribute("onclick", "ratingsSet("+i+","+x+");");
具體地說,我被+i+
和+x+
扔掉了。
起初我以爲這些是增量操作符的某種變體,但後來決定它必須連接某些東西,但我無法弄清楚是什麼/如何。即得到由循環生成的HTML是:
<span class="star" onclick="ratingsSet(0,0);">*</span>
<span class="star" onclick="ratingsSet(0,1);">*</span>
<span class="star" onclick="ratingsSet(0,2);">*</span>
<span class="star" onclick="ratingsSet(0,3);">*</span>
但我的逆向工程印章都沒有我(如果我有任何開始與)。
幫助?
僅供參考,使用setAttribute方法添加單擊事件處理函數在某些瀏覽器中不起作用。使用'star.onclick = function(){...};'。 – duri 2011-05-03 11:35:44
設置點擊事件是一種非常糟糕的方式。 – Rudie 2011-05-03 11:36:15