好吧,這可能是一個容易的,我只是不知道該怎麼做。我有一個主要的功能,它在一個「ID」。此ID是唯一的,我想將它傳遞給另一個爲我計數的函數,並將該計數返回給innerHtml span標記。Javascript將一個變量從函數調用傳遞給innerHtml
之所以這樣,是因爲我可以在同一時間有這些開放的5,但它們將具有相同的跨度ID名稱「editCommentsCounter」 ......我希望他們能像「editCommentsCounter-ID」
function editCommentToggle(id)
{
theRow = document.getElementById("id"+id);
//user = theRow.cells[0].innerHTML;
//date = theRow.cells[1].innerHTML;
com = theRow.cells[2].innerText ;
comLength = theRow.cells[2].innerText.length ;
idx = 2;
maxlength = 250;
count = maxlength - comLength;
// Comment field
cell = theRow.cells[idx];
while(cell.childNodes.length > 0) cell.removeChild(cell.childNodes[0]);
spanTag = document.createElement("span");
spanTag.innerHTML = "You have <strong><span id='editCommentsCounter'>"+count+"</span></strong> characters left.<br/>";
cell.appendChild(spanTag);
element = document.createElement("textarea");
element.id="commentsTextArea-"+id;
element.rows="3";
element.value = com;
element.style.width = "400px";
element.maxLength = "250";
element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounterEdit('editCommentsCounter', maxlength, this);};
cell.appendChild(element);
}
基本上,我想借此:
spanTag.innerHTML = "You have <strong><span id='editCommentsCounter'>"+count+"</span></strong> characters left.<br/>";
,並作出這樣的成類似`跨度ID = 'editCommentsCounter-' + ID
所以當我把這個:
element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounterEdit('editCommentsCounter', maxlength, this);};
我editCommentsCounter稱這上面連接到它
該ID看看林說什麼嗎?
有人喜歡污染全局命名空間。使用var! – epascarello 2012-07-16 14:57:18