好吧,这可能是一个容易的,我只是不知道该怎么做。我有一个主要的功能,它在一个“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